5

For my sails app I'm using the following code to unit test the User model but got the error message:

'TypeError: Object # has no method 'create''

var User = require('../../api/models/User');
var Sails = require('sails');

console.log(User);

describe("User Model:", function() {  

  // create a variable to hold the instantiated sails server
  var app;

  // Global before hook
  before(function(done) {

    // Lift Sails and start the server
    Sails.lift({

      log: {
        level: 'error'
      },

    }, function(err, sails) {
      app = sails;
      done(err, sails);
    });
  });

  // Global after hook
  after(function(done) {
    app.lower(done);
  });

  describe("Password encryption", function() {

    describe("for a new user", function() {
      var user;
      before(function (cb) {
        var userData = {
          email: "testuser@sails.com",
          password: "test_password",
          passwordConfirmation: "test_password"
        };

        User.create(userData, function (err, newUser) {
          if (err) return cb(err);
          user = newUser;
          cb();
        });
      });

      it("must encrypt the password", function() {
        user.must.have.property('encryptedPassword');
        user.must.not.have.property('password');
        user.must.not.have.property('passwordConfirmation');
      });

      after(function (cb){
        user.destroy(function (err) {
          cb(err);
        });
      });
  });

As it seems to me that sails is correctly lifted, what is the problem causing the create method not to be available ?

Luc
  • 16,604
  • 34
  • 121
  • 183
  • I am quite new to nodejs and trying to write some tests for an application that I am building. Would it be possible to have a look at your model, so that I can try and figure out how to write the tests for my models please. Thanks – kushaldsouza Mar 01 '14 at 00:50
  • unit tests should not spin up server or hit databases/do i/o – imsheth May 20 '20 at 02:18

1 Answers1

15

Remove the first line:

var User = require('../../api/models/User');

Once Sails app is lifted, you will have your models available automatically, see here, for example.

And in your case, your first line overrides the User model which would be otherwise constructed by Sails.js, that's why even though you have an object it's not a Waterline model.

bredikhin
  • 8,875
  • 3
  • 40
  • 44
  • 1
    I am facing same problem if I define test.js file under Test folder(Under any folder).Unable to reach User model ,shows Error"ReferenceError: User is not defined ".If i write var User = require('../api/models/User.js'); shows error "TypeError: Object # has no method 'create' " – tajuddin Mar 15 '14 at 07:42
  • @tajuddin Please look at [this example](https://github.com/bredikhin/sailsjs-mocha-testing-barrels-fixtures-example/blob/master/test/index.js). – bredikhin Mar 15 '14 at 15:23
  • 1
    @FrancescoGramano you can try checking out [the whole repo](https://github.com/bredikhin/sailsjs-mocha-testing-barrels-fixtures-example) (it's been restructured) or also [this one](https://github.com/bredikhin/sails101-testing) – bredikhin Mar 01 '15 at 20:01