FAQ
Hi!

I'm having problems getting up and running with testing my node.js code.

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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Eldar at Mar 24, 2012 at 8:20 am
    As far as I know sinon.js <http://sinonjs.org/> is the best util for doing
    such kind of things.

    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Lundberg at Mar 24, 2012 at 9:58 am

    On Saturday, March 24, 2012 5:17:24 AM UTC+1, Eldar wrote:
    As far as I know sinon.js <http://sinonjs.org/> is the best util for
    doing such kind of things.

    Hello Eldar,

    I'm sorry I don't want to seem ungrateful for your answer but it doesn't
    help much. I've read many articles about mocking and I've went throught the
    sinon.js documentation but all of the docs and articles are to basic or
    abstract so I don't understand how to do what I explained in the first post.

    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Cooper at Mar 24, 2012 at 5:25 pm

    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

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Nuno Job at Mar 24, 2012 at 5:58 pm
    Nock is what you are looking for:


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


    Nuno
    On Sat, Mar 24, 2012 at 5:25 PM, 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

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Lundberg at Mar 24, 2012 at 10:31 pm

    On Saturday, March 24, 2012 6:57:20 PM UTC+1, Nuno Job wrote:
    Nock is what you are looking for:


    - http://github.com/flatiron/​nock <http://github.com/flatiron/nock>


    Nuno
    Have you actually read the discussion before you answered? :) Nock seems to
    be for mocking http requests which is not what I need right now. It seems
    nice for its purpose though.

    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Lundberg at Mar 24, 2012 at 10:29 pm

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rehan Iftikhar at Mar 25, 2012 at 5:51 am
    Hi Martin

    I was asking myself these very same questions about a year ago. What
    really helped me get my head around unit testing was to read Misko
    Hevery's guide to testable code: http://bit.ly/GNymAe. While the
    examples are in Java, the same principles apply: embrace the Single
    Responsiblity Principle (SRP), leverage Dependency Injection (DI), and
    isolate logic using Mock Objects.

    I test-drove an example of a UserAuthenticator class using these ideas
    here: http://bit.ly/GNmDeQ. I tried to emulate your dependencies as
    much as possible (UserRepository class). It uses Jasmine for the unit
    tests. You
    can use jasmine-node (https://github.com/mhevery/jasmine-node) to run
    these locally.

    -Rehan
    On Friday, March 23, 2012 3:35:03 PM UTC-7, Martin Lundberg wrote:

    Hi!

    I'm having problems getting up and running with testing my node.js code.

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Martin Lundberg at Mar 25, 2012 at 8:49 pm

    On Sunday, March 25, 2012 5:39:23 AM UTC+2, Rehan Iftikhar wrote:
    Hi Martin

    I was asking myself these very same questions about a year ago. What
    really helped me get my head around unit testing was to read Misko
    Hevery's guide to testable code: http://bit.ly/GNymAe. While the
    examples are in Java, the same principles apply: embrace the Single
    Responsiblity Principle (SRP), leverage Dependency Injection (DI), and
    isolate logic using Mock Objects.

    I test-drove an example of a UserAuthenticator class using these ideas
    here: http://bit.ly/GNmDeQ. I tried to emulate your dependencies as
    much as possible (UserRepository class). It uses Jasmine for the unit
    tests. You
    can use jasmine-node (https://github.com/mhevery/​jasmine-node<https://github.com/mhevery/jasmine-node>)
    to run
    these locally.

    -Rehan
    Hi Rehan! Thanks for the link and code, I'll definitely read throught the
    guide by Misko Hevery and look through your code. I see that you use events
    for almost everything, do you believe that you should use events as much as
    possible instead of callbacks?

    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rehan Iftikhar at Mar 27, 2012 at 11:34 pm
    Martin

    I have come to prefer that my objects emit events rather than take
    callbacks in method invocations. Some benefits I have seen are:

    - prevents nested callback spaghetti code
    - feels more like an interface (and therefore feels easier to unit test)

    Im sure there are some downsides, but Im relatively new to the language
    myself.

    -Rehan

    On Sunday, March 25, 2012 1:48:53 PM UTC-7, Martin Lundberg wrote:
    On Sunday, March 25, 2012 5:39:23 AM UTC+2, Rehan Iftikhar wrote:

    Hi Martin

    I was asking myself these very same questions about a year ago. What
    really helped me get my head around unit testing was to read Misko
    Hevery's guide to testable code: http://bit.ly/GNymAe. While the
    examples are in Java, the same principles apply: embrace the Single
    Responsiblity Principle (SRP), leverage Dependency Injection (DI), and
    isolate logic using Mock Objects.

    I test-drove an example of a UserAuthenticator class using these ideas
    here: http://bit.ly/GNmDeQ. I tried to emulate your dependencies as
    much as possible (UserRepository class). It uses Jasmine for the unit
    tests. You
    can use jasmine-node (https://github.com/mhevery/​jasmine-node<https://github.com/mhevery/jasmine-node>)
    to run
    these locally.

    -Rehan
    Hi Rehan! Thanks for the link and code, I'll definitely read throught the
    guide by Misko Hevery and look through your code. I see that you use events
    for almost everything, do you believe that you should use events as much as
    possible instead of callbacks?
    --
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Rehan Iftikhar at Mar 25, 2012 at 5:51 am
    Hi Martin

    I was asking myself these very same questions about a year ago. What
    really helped me get my head around unit testing was to read Misko
    Hevery's guide to testable code: http://bit.ly/GNymAe. While the
    examples are in Java, the same principles apply: embrace the Single
    Responsibility Principle (SRP), leverage Dependency Injection (DI), and
    isolate logic using Mock Objects.

    I test-drove an example of a UserAuthenticator class using these ideas
    here: http://bit.ly/GNmDeQ. I tried to emulate your dependencies as
    much as possible (UserRepository class). It uses Jasmine for the unit
    tests. You
    can use jasmine-node (https://github.com/mhevery/jasmine-node) to run
    these locally.

    -Rehan
    On Friday, March 23, 2012 3:35:03 PM UTC-7, Martin Lundberg wrote:

    Hi!

    I'm having problems getting up and running with testing my node.js code.

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedMar 23, '12 at 10:35p
activeMar 27, '12 at 11:34p
posts11
users5
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase