mardi 23 décembre 2014

Sails.js unit-test

I am building web application using Sails.js.


and I also building unit-test environment based on this site. (http://ift.tt/1AUCQMg)


When I try to Controller test below, message says like this. ※ I am using wolfpack as testing framework.



1) The UserController should return a HTTP 200 response if the user was added successfully:
ReferenceError: runs is not defined
at Context.<anonymous> (my_project_path/test/controllers/UserController.spec.js:42:9)

2) The UserController #create ユーザーが作成されるはず:
ReferenceError: sails is not defined
at Object.module.exports.create (my_project_path/api/controllers/UserController.js:9:457)


I don't know why error message says " ReferenceError: runs is not defined" or "ReferenceError: sails is not defined" because I uncommented sails:true in global.js for global use.


please anyone help me. (and I apologize for my bad English... I am learning English now ;_;)


here is my Controller test code.



var UserController = require('../../api/controllers/UserController'),
sinon = require('sinon'),
constant = require('../../config/const'),
User = require('../../api/models/User'),
wolfpack = require('wolfpack');

global.User = wolfpack(User);

var request = {
params: {
username: 'testuser',
email: 'testuser@test.com',
familyname: 'test',
firstname: 'user',
gender: constant.constVal.gender.male,
subscription: true,
term: true
}
};

var response = {
send: sinon.stub()
};

describe('The UserController', function() {
describe('#create', function() {
it('ユーザーが作成されるはず', function() {
UserController.create(request, response);

expect(User.username.lastCall.args[0]).toBe(request.params.username);
expect(User.email.lastCall.args[0]).toBe(request.params.email);
expect(User.familyname.lastCall.args[0]).toBe(request.params.familyname);
expect(User.firstname.lastCall.args[0]).toBe(request.params.firstname);
expect(User.gender.lastCall.args[0]).toBe(request.params.gender);
expect(User.subscription.lastCall.args[0]).toBe(request.params.subscription);
expect(User.term.lastCall.args[0]).toBe(request.params.term);
})
});

it("should return a HTTP 200 response if the user was added successfully", function() {
// Run first part of test asynchronously (jasmine function)
runs(function() {
UserController.create(request, response);
});

// When the callback executes, it should call res.send, so we should wait for it
waitsFor(function() {
return response.send.called;
});

// Now we can test if the proper code was sent
runs(function() {
expect(response.send.lastCall.calledWith(200)).toBeTruthy();
});
});
});

1 commentaire:

  1. Hello. From looking at the test environment, it seems you are using the Mocha testing framework rather than Jasmine test framework. "runs" is a Jasmine function for asynchronous testing. That's why you are getting the runs is not defined, because Mocha uses a different function.

    Regarding sails with wolfpack you test each file individually. You are, for example, loading the controller, the model, etc. separately, without calling sails at all. However, there might be places in your code where you are using a sails feature, for example the Logger or the config files. Since Sails is not loaded with Wolfpack, and nowhere in your code or tests you are defining the sails global, then you get that error.

    Try defining it in your test as a global, and stub or mock any functions you code might use from it:

    GLOBAL.sails = {
    config: {},
    log: sinon.stub()
    }

    RépondreSupprimer