sails.js - Unit testing Sails/Waterline models with mocha/supertest: toJSON() issue -


I am setting up a unit test on the models, controllers and services of my cell application. While examining my user model, I stumbled upon a misleading issue. Version of User.js :

  module.exports = {attributes: {username: {type: 'string', required: true}, [... Other features ...], admin: {type: 'boolean', default:: wrong}, toJSON: function () {var obj = this.toObject (); // Do not send back admin attribute obj.isAdmin; Obj.updatedAt delete; Return obj; }}}  

The following is my test.js , which means to walk with Mocha. Note that I turned on the plurality flag in blueprint config. Also, I use Amber for data-correction blueprints. Therefore, my request to {user: {...}} .

  // should look like the application factory var sails = require ('sails / lib / app)'); Var assert = Required ('Thrust'); Var Requests = Required ('Superstest'); // Instance the example of a loose app we will use the var app = cell (); Var user; First (Function (done) {// Lift Sales and App Store Store app. Lift ({Global: True, // Load almost everything but loads policies: ['Moduloder', 'User config' 'ORM', 'HTP', 'Controller', 'Services', 'Request', 'Reactions', 'Blueprint'],}, Function () {user = app.models.user; console.log Gone! '); Done ();});}); // function (function (done) {app.lower (done);}); Description ('user', function) (describe ('.update'), function () (this should be 'Admin attribute change', function (done) {User.findOneByUsername ('skippy'). Exec function ( Error, user) {if (mistake) new error ('user not found'); user.isAdmin = false; request (app.hooks.http.app). Input ('/ users /' + user.id) .end ({User: user}). (200). Expect ('content-type', / json /) .end (function () {user.findOneByUsername ('skippy'). Exec (function (mistake, user) {assert .equal (User.isAdmin, false); done ();}}}}}}}}}}}}}}};  

preventing write facility on user.isAdmin Before installing any policy, I hope that my user.isAdmin attribute will be updated by this request. Before running the test, my user's admin flag is set to true. Running the test shows that flag updates Not done:

  1) User should modify the .update () Admin attribute: may be unblocked Asserti OnError: true == false  

Runs on the client side running on Quality Test, is also more shocked to run on the quality test, although it updates the admin's entitlement, though not Can tell if it was updated because I am removing the payload in user. ().

  var user; Modules ("user", {setup: function (emphasize) {stop (2000); // authenticate with user $ skippy $ .post ('/ auth / local', {identifier: 'skippy', password: ' Guru-meditation!!}, Function {user = data.user;}) Always (Quick Start);}, Teadown: Function (emphasis) {$ .get ('/ logout', function) {});}}); AsyncTest ("With Peat / ISDMN attribute, the user should convert it to DB and return the user", function () (stop (1000); user.isadmin = true; $ .gend ({url: '/ user / '+ User .id, type:' put ', data: {user: user}, success: function (data) {console.log (data); // I can not test admin value here equals (data.user. FirstName, user first name, "first name should not be modified"); ();}, Error: function (cause) {equal (cause type, 'object', 'cause of failure should be an object'); start ();}});});  

In the Mongodi console:

  & gt; Db.user.find ({user name: 'skipi'}); {"_id": ObjectId ("541d9b451043c7f1d1fd565a"), " Aizman ": Wrong, ...," Username ":" Skippy "}  

Still more dangerous, this is to comment obj.isAdmin In the user.toJSON () passes the Mocha exam!

So, I wonder:

  • Line model toJSON () method is used only for output filtering? Or does it have any effect on writing operations like update ().
  • Is this problem related to the Supreme? Since jQuery.ajax () improves the isAdmin flag in my quintet test, it is quite strange that the supertest does not request

Any suggestions really appreciated.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -