SAILSJS Framework for a period of time, feel if the function is complex, very difficult to deal with, want to use the unit test to see if it can solve the problem. The Official document of SAILSJS uses mocha, I searched for some information, mainly referring to Pauling's "simple Nodejs" unit test chapter, SAILSJS's official documentation, and an example on GitHub https://github.com/ Bredikhin/sailsjs-mocha-testing-barrels-fixtures-example. 1) The system environment is as followsWindows 8.1 X64Nodejs 0.12.1Sailsjs 0.11.02) installation processneed to install Mocha and Should,mocha are test frameworks, should is an assertion, or you can use native assert,supertest to test the HTTP request, which is used to test the controllernpm Install mocha-gNPM Install shouldnpm Install SuperTestwhere Mocha's homepage is Http://mochajs.org/,should's home page is https://www.npmjs.com/package/should, which can be found in the relevant documents from these sites. Mocha is installed as a global command, you can run Mocha directly on the command line, its parameters are many, you can use Mocha--help view parameters. 3) directory and file preparation
./myApp├── api├── assets├── ...├── test│ ├── unit│ │ ├── controllers│ │ │ └── UserController.test.js│ │ ├── models│ │ │ └── User.test.js│ │ └── ...│ ├── fixtures│ ├── ...│ ├── bootstrap.test.js│ └── mocha.opts└── views
first build the test directory under the root directory, and then create the test\unit\controllers and Test\unit\models directories at once. then set up the file Bootstrap.test.js under test, this file is used to start the SAILSJS program, also can be said to configure the environment, the content is as follows
var Sails = require (' Sails '), sails; before (function (done) { Sails.lift ({ log: { level: ' ERROR '//Specify error levels to avoid debug output, which is primarily used to debug controller } }, function (err, server) { sails = server; if (err) return done (err); //Here you can load fixtures, etc. Done (err, sails); }); }); After (the function (done) { var done_called = false; Sails.lower (function () { if (!done_called) { done_called = true; setTimeout (function () { sails.log.debug ("Inside App.lower, callback not called yet. Calling. "); Done (); }, 1000); } else { sails.log.debug ("Inside App.lower, callback already called."); } }); }); It is important to note that after () and official documents are inconsistent because there is a problem with the done () call multiple times, so I refer to a resource on the net and increase the wait.
4) test controller (Controller)
set up UserController.test.js under Test\unit\controllers, the content is as follows
var request = require (' SuperTest ');
var should = require (' should ');
describe (' Userscontroller ', function () {
describe (' #login () ', function () {
it (' should get 1 ', function (done) {
request (Sails.hooks.http.app)
. Get ('/user/login?login_code=12345678901&user_password=111111 ')
. End (function (err, results) {
should (Results.res.body.result). be.exactly (1);
Done
();
});
});
});
});
Here is the BDD style, I prefer this way, also can adopt the style of TDD, feel almost
The above code is also very simple, the/user/login?login_code=12345678901&user_password=111111 initiated the request, according to the return content to judge, here to pay attention to, Return more information than usual return, such as my return is similar to {result:1, desc: ' Login success ', but SuperTest returned much more, So use Result.res.body.result to get the result value, you can also print out the information to look at, but note that because the previous set Log:{level: ' Error '}, you can not see the output, you can change the error to info, Try again.
5) Run the test
mocha-t 10000 test/bootstrap.test.js test/unit/**/*.test.jsYou can also add the NPM command under scripts in Package.json, as follows
"Scripts": { "start": "Node App.js", "Debug": "Node Debug app.js", "test": "Mocha -T 10000 test/bootstrap.test.js test/unit/**/*.test.js " } so that you can run NPM test directly to perform the tests. to illustrate, the-t 10000 represents an increase in the execution time of each test to 10 seconds, the default is 2 seconds, on my system can not be returned, there will be an error, maybe my machine is too slow. returns 1 passing (7s) If the user name password is correct
If an error occurs, the return
0 Passing (6s) 1 failing 1) userscontroller #login () should get 1: uncaught assertionerror:expected 0 to be 1 + expected-actual -0 +1 At test.<anonymous> (D:\work\nodejs\KangfuMobile\test\unit\controllers\Use rcontroller.test.js:13:40) at net.js:1392:10
so it's very easy to test the controller,6) test logic, establish User.test.js in Test\unit\models, content is
var should = require (' should '); var bcrypt = require (' Bcrypt '); describe (' User table ', function () { describe (' Find user information ', function () { It (' should check if user table is not this user ', function (done) { User.find ({ user_code: ' 12345678901 ' }). exec (function (err, results) { should (Results.length). be.exactly (1); Done (); }); }); }); describe (' Find User name password ', function () { It (' should check for password entry error, or password encryption system error ', function (done) { User.findone ({ user_code: ' 12345678901 ' }). exec (function (err, user) { var user_password = ' 111111 '; var user_password_o = User.user_password; user_password_o = "$2a$" + user_password_o.substr (4); Bcrypt.compare (User_password, user_password_o, function (err, comp) { should (comp). be.exactly (true); Done (); }); }); }); }); }); There are two tests above, one is to see if there is one user named "12345678901" and the second is to see if the password is correct. run the test command to get the following result, and if there are no users, the following error will occur: 2 Passing (6s) 3 Failing 1) Userscontroller #login () should get 1: uncaught assertionerror:expected 0 to be 1 + expected-actual -0 +1 At test.<anonymous> (D:\work\nodejs\KangfuMobile\test\unit\controllers\UseRCONTROLLER.TEST.JS:13:40) At net.js:1392:10 2) User table Find user information should check whether the user table does not have this user: uncaught assertionerror:expected 0 to be 1 + expected-actual -0 +1 At d:\work\nodejs\kangfumobile\test\unit\models\users.test.js:10:31 At Readableaddchunk (_stream_readable.js:163:16) At Socket.Readable.push (_stream_readable.js:126:10) At Tcp.onread (net.js:529:20) 3) User table look up user name password should check the password is wrong, or password encryption system error: uncaught typeerror:cannot Read property ' User_password ' of undefined At d:\work\nodejs\kangfumobile\test\unit\models\users.test.js:21:31 At Readableaddchunk (_stream_readable.js:163:16) At Socket.Readable.push (_stream_readable.js:126:10) At Tcp.onread (net.js:529:20) you can see if an error has occurred and check for possible problems, and if correct, you will
5 Passing (6s)7) Basically, the basic content of unit testing is clear, but want to better use, also need to learn how to use assertions, such as should, also need to learn SuperTest grammar, Mocha BDD style is very simple, isdescribe (' test content ', function () {describe (' Test content 1 ', function () {it (' What needs attention ', function (done) {//Do testDone (); }) }) })If the async framework is used, it is very familiar with the use of done (), which is primarily used to handle asynchronous problems until the done () return is finished. The use of should is very simple, you can refer to the official documentation, you can also look at the example of the Ox man: https://github.com/shouldjs/examples
Nodejs Study Notes (iv)---unit test