balanv
balanv

Reputation: 10898

Ajax testing with Mocha - Nodejs

I tried using Mocha to write tests for one of my application. I can able to run different scenarios when page reloads.

Consider the below scenario

1) A user enters value in a text field "Username"

2) Clicks "Save" button

3) This will send ajax call to save user name and response might come back in 1 to 2 seconds

4) A message is shown after response is received

How can i implement test for above scenario?

NOTE : I tried the below code, but the browser isn't waiting at all

  describe("Ajax test", function(){

        before(function(){
            return this.browser.field('save').click() 
        })

    it("Tests save username", function(){

        this.browser.wait(6000, function() {
          //code to check if success message is shown in body
        })
    })
  })

thanks, Balan

Upvotes: 1

Views: 878

Answers (1)

Tyler
Tyler

Reputation: 18177

There is a callback you can make in each test case, and you should wait for a response from the server before making this callback.

The pattern should be something like this:

// Test case.  Notice the `done` param
it('Should make an async call', function(done){

    // Make an async call, with a callback function
    async_call(function(){

        // Do whatever you need (test some values)
        // Then call done
        done();
    });
});

Upvotes: 2

Related Questions