bredikhin / sailsjs-mocha-testing-barrels-fixtures-example

An example of Sails.js-project testing with Mocha and Barrels for fixtures
121 stars 22 forks source link

Need update for v0.10.x + latest barrels api? #2

Closed jgod closed 10 years ago

jgod commented 10 years ago

I'm struggling with latest Barrels and latest Sails integration -- following method here to split up tests.

Here's what I'm trying:

testHelper:

var Sails = require('sails');
var Barrels = require('barrels');
var async = require('async');
var path = require('path');

global.assert = require('assert');
global.barrels = new Barrels(path.resolve(__dirname, 'fixtures/'));
global.sinon = require('sinon');
global.fixtures = barrels.data;
global.mockRequest = {};
global.mockResponse = {
  ajaxRequired: sinon.spy(),
  badRequest: sinon.spy(),
  dbError: sinon.spy(),
  forbidden: sinon.spy(),
  notFound: sinon.spy(),
  ok: sinon.spy(),
  serverError: sinon.spy(),
  unauthorised: sinon.spy(),
  json: sinon.spy()
};
global.responseKeys = Object.keys(mockResponse);

// Global before hook
before(function (done) {
  // Lift Sails with test database.
  console.log('Before all tests - lifting sails...');
  Sails.lift({
    log: {level: 'error'},
    adapters: {default: 'test'}
  }, function(err, sails) {
    if (err) return done(err);

    // Load fixtures
    barrels.populate(function(err) {
      done(err);
    });
  });
});

// Global after hook
after(function (done) {
  console.log('After test - lowering sails...');
  sails.lower(done);
});

Model:

require('../testHelper');

// Instance method testing
var attrs = require("../../api/models/User").attributes;
var StripSecrets = attrs.stripSecrets;

describe('User', function () {
  beforeEach(function () {
    mockRequest = {
      session: {},
      body: {},
      wantsJSON: true,
      flash: {}
    };
  });

  afterEach(function () {
    for (var i = 0; i < responseKeys.length; i++ ) {
      // console.log("resetting response." + responseKeys[i]);
      mockResponse[responseKeys[i]].reset();
    }
  });

  describe('stripSecrets()', function () {
    it('should not contain email', function (done) {
      User.find(function (err, users) {
        if (err) return done(err);
        assert(users && users.length, 'Users should exist!');

        var user = users[0];
        StripSecrets(user);
        assert(user.hasOwnProperty('email'), false);
        done();
      })
    });

    it('should not contain password', function (done) {
      User.find(function (err, users) {
        if (err) return done(err);
        assert(users && users.length, 'Users should exist!');

        var user = users[0];
        StripSecrets(user);
        assert(user.hasOwnProperty('password'), false);
        done();
      })
    });
  })
});

Printing out users gives me my mock fixtures, but when I attempt User.find it always hits the assertions (no users are returned).

jgod commented 10 years ago

Besides some little bugs in there, I was using the wrong version of the name for my mocks (I have pluralize on)... users.json -> user.json.

Looks like it's working now :+1: