Understanding Unit Tests for sails.js

Asked 1 years ago, Updated 1 years ago, 69 views

I asked a question at the head office, but I would also like to ask a question in the Japanese version. I am currently creating a web application using sails.js. By default, sails.js does not have a test environment, so we created a test environment based on this site. (http://thenullpointer.in/2014/09/08/Testing-and-code-coverage-using-grunt-mocha-istanbul-in-Sails-js/index.html) Then I installed wolfpack in the testing framework and created the following code, and when I ran it, I got the following message and got an error.

 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 user should be created:    
ReferenceError: sails is not defined 
at Object.module.exports.create(my_project_path/api/controllers/UserController.js:9:457)

The controller test codes are as follows: As I have removed the comments on sails:true in global.js, I should be able to use the import declaration of sails globally, but can't I use it below the test folder? Also, do I need any settings to make it available in the test folder?

I look forward to your kind cooperation.

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: '[email protected]',
        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('User should be created', 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 execs, it should call res.send, so should wait for it
        waitsFor(function(){
            return response.send.called;
        });

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

javascript test node.js

2022-09-30 10:38

1 Answers

I'm sorry.It's self-less. ReferenceError: For sails is not defined, it was UserController.js instead of UserController.spec.js for testing.However, UserController.js is global and says sails:true, so you shouldn't get in trouble even if you don't declare import, but if you add require('sails), you'll get in trouble.

Also, regarding ReferenceError: runs is not defined, my test uses mocha and this code (sample code is used) was for jasmine, so I thought they didn't know about runs.

How do I write mocha?


2022-09-30 10:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.