Nock is what you are looking for:

   - http://github.com/flatiron/nock


Nuno

On Sat, Mar 24, 2012 at 5:25 PM, Martin Cooper <[email protected]> wrote:

> On Fri, Mar 23, 2012 at 3:35 PM, Martin Lundberg
> <[email protected]> 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
>
> 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
>
>
> > I've got a file called auth.js with a function that looks like:
> > authenticate(email, password, callback). The authenticate function tries
> to
> > fetch a user with the given e-mail using a collaborator UserRepository
> > object. It runs a function like: user = users.get({ email: email },
> > function(user) { ... }) where users is the UserRepository object. If it
> gets
> > a user and the password is correct it calls the callback function like:
> > callback(null, user) but if it don't get a user or if the password is
> > incorrect it calls the callback sending an error as the first parameter.
> >
> > When writing unit tests the authenticate method (and all the other
> functions
> > in auth.js) I don't want to use the real UserRepository (which in turn
> uses
> > a database library like node-mysql) object and a real database. I
> believe I
> > should create a stub and define return values so I can see that
> authenticate
> > returns correct values when it gets a user and when it don't. The
> problems
> > is that I've read a lot of tutorials and articles about mocking and
> testing
> > but I still don't understand how I should do this in a good way.
> >
> > I hope the text isn't too confusing. I would really appreciate some
> guiding
> > in this area so I can get started testing my apps in a good way.
> >
> > Regards,
> >
> > Martin Lundberg
> >
> > --
> > 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
>
> --
> 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
>

-- 
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

Reply via email to