Nodejs Study notes (v)---unit test supplement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.