Created
March 27, 2015 08:51
-
-
Save luiselizondo/39872efc716daa878cb9 to your computer and use it in GitHub Desktop.
How to mock a service in Sails, found at https://github.com/fdvj/wolfpack/issues/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sinon = require('sinon'), | |
wolfpack = require('wolfpack'); | |
// Mock EmailSender | |
global.EmailSender { | |
sendEmailToUser: sinon.stub() | |
}; | |
// Instantiate you model | |
global.User = wolfpack('path_to_models/User'); | |
// Load your controller | |
var UserController = require('path_to_controllers/UserController'); | |
describe('UserController', function() { | |
describe('#register', function(){ | |
var req, res; | |
beforeEach(function(){ | |
// Mock your response obj | |
res = { | |
json: sinon.stub() | |
}; | |
}); | |
it("should send an email to the user", function(){ | |
// Mock you request object so that it passes your validations | |
req = { | |
body: { | |
email: '[email protected]', | |
password: 'F734nmh!.rue', | |
confirmPassword: 'F734nmh!.rue' | |
} | |
}; | |
var user = req.body; | |
// Since you are testing create, User.find shouldn't find anything, | |
// so clear any wolfpack results just in case | |
wolfpack().clearResults(); | |
// Now set the results for your create | |
wolfpack().setCreateResults(user); | |
// Now we can do our asynchronous test | |
runs(function(){ | |
UserController.register(req, res); | |
}); | |
waitsFor(function(){ | |
return User.create.called; | |
}); | |
runs(function(){ | |
// Now we can test if the proper arguments were passed to your | |
// EmailSender service | |
var params = EmailSender.sendEmailToUser.lastCall.args; | |
expect(params[0].email).toBe(user.email); | |
expect(params[1]).toBe('activateNewUser'); | |
// etc... | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment