Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mocking web request in Javascript

    I am working on writing a Qunit test for my javascript function. My function has a if statement like this:
    if(window.location.pathname.toLowerCase() == "/search/home.html" )
    {.........................}
    Everything I do is inside this if statement. I need to find a way how I can mock the window.location.pathname from my test case. I would highly appreciate your suggestions.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Simply set up the script so that it runs from a file called home.html in a folder called search in the root directory of your hard drive. That way you don't have to mock the path as it will actually be that.

    Alternatively install a web server such as WAMP or XAMPP onto your computer and then you can put the search folder inside whatever folder you tell the web server is the root directory.

    If you are just after a quick way to test the code inside the if statement then comment out the if statement so the code always runs.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •