This morning continue to study mocha, suddenly found a problem, most of my programs are required to login verification, so need to pre-login before the next test, began to find the answer online, found no such information, very confused, and finally found himself too stupid, Because this problem is too simple in node, the solution is as follows
Modify the Bootstrap.test.js as follows
var Sails = require (' Sails '), Sails;var request = require (' SuperTest '); var Port = 1447; Test Boot Port Agent = request.agent (' http://localhost: ' +port); Server connection, global variable before (function (done) {Sails.lift ({log: ') ': ' Error '},port:port}, function (err, server) {Sails = Server if (err) {return done (err);} else {//Login agent.get ('/user/login?login_code=18875282207&user_password=111111 '). End (function (err, res) {if (err) return done (ERR);d One (err, sails);});});}); After (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.");});
Added global variable agent = request.agent (' http://localhost: ' +port), this variable can be initiated at a later request, at the same time start Sailsjs, directly log in, and later test controllers, Just call Agent.get () or Agent.post (), and the previous controller test UserController.test.js is modified as follows:
var should = require (' should ');d escribe (' Userscontroller ', function () {Describe (' #session () ', function () {It (' should Get True ', function (done) {agent.get ('/user/test '). End (function (err, results) {should ( results.res.body.authenticated). be.exactly (True);d one ();});});
Where the return value of/user/test is in the format {Authenticated:true} and returns true if already logged in, otherwise false.
Run NPM test with the following results
1 Passing (8s)
It is easy to solve, write down the memo.
Nodejs Study notes (v)---unit test supplement