On Saturday, March 24, 2012 6:25:35 PM UTC+1, Martin Cooper wrote:
>
> On Fri, Mar 23, 2012 at 3:35 PM, Martin Lundberg wrote:
> > Hi!
> >
> > I'm having problems getting up and running with testing my node.js code.
>
> There are many ways to do this, depending, to some extent, on how you
> choose to write your tests. One option would be to use Mockery with
> your choice of unit testing framework:
>
> https://github.com/mfncooper/mockery<https://github.com/mfncooper/mockery>
>
> Let's assume that your auth.js module looks more or less like this:
>
> var users = require("./users.js");
>
> exports.authenticate = function(email, password, callback) {
> // ...
> }
>
> Then in your unit test module (say auth.test.js), you might create a
> super-simple mock / stub and use it something like this. (The actual
> code will depend on which unit test framework you're using.)
>
> var usersMock = {
> get: function (userObj, cb) {
> // Do whatever you need in your mock 'get'
> }
> };
>
> // Then in your test case code:
>
> setup: function () {
> mockery.enable();
> mockery.registerMock("./users.js", usersMock);
> }
>
> teardown: function () {
> mockery.deregisterAll();
> mockery.disable();
> }
>
> testAuthGet: function () {
> var auth = require("path/to/auth.js");
> // whatever other prep work you need
> auth.authenticate(email, pwd, function (err, user) {
> // whatever assertions you need to verify the results
> });
> }
>
> This works because Mockery will load your mock / stub when auth.js
> tries to require() the users.js module, so when your test calls
> authenticate(), that invocation will be using your mock users.get()
> instead of the real one.
>
> Hope that helps.
>
> --
> Martin Cooper
>
Mockery seems really nice for the dependencies the modules fetch using
require, thanks Martin!
Howver, right now I'm sending the dependencies into the module like
"module.exports = function(users) {}" since the users object needs to be
initialized before sent to the auth module and it seems to me that the
simples way which works great is by just creating a simple stub like your
usersMock: var usersStub = { get: function() {...}} and send it into the
auth module like var auth = require('./auth')(usersStub).
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en