vendredi 26 août 2016

run Jasmine tests after initial function calls

I'm writing Jasmine tests for a Node.js API. I'm trying to test the create a user functionality. The test would look like this:

describe('User test', function() {
  describe('Post /api/saveUser'), function() {
    it('saves a new user', function(done) {
      request.post({url: 'http://website/api/saveUser', form:{email:testU.email, password:testU.password}, headers: {'authorization': adminU.jwt}}, function(err, res, body) {
        expect(res.statusCode).toBe(200);
      });
    });
  });
});

So I need to authenticate the admin user (adminU) in the spec to get the valid token to pass in the request. This is done using another endpoint.

request.post({url: 'http://website/api/authenticate', form:{email:adminU.email, password: adminU.password}}, function(err, res, body) {
  adminUser.jwt = JSON.parse(res.body).token;
});

But how do I combine these. If I slot the authentication code above the User test block, the user tests are run before the response from the authentication endpoint is received. The obvious choice is to wrap the user tests in the callback from the authenticate api, but then when I run the tests Jasmine doesn't actually run them.

What's the best way to use the results of a callback in the Jasmine specs?

Aucun commentaire:

Enregistrer un commentaire