Closed jgod closed 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).
users
User.find
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.
users.json
user.json
Looks like it's working now :+1:
I'm struggling with latest Barrels and latest Sails integration -- following method here to split up tests.
Here's what I'm trying:
testHelper:
Model:
Printing out
users
gives me my mock fixtures, but when I attemptUser.find
it always hits the assertions (no users are returned).