Mocking Postgres Database calls for unit testing

1,144 views
Skip to first unread message

Modika

unread,
Oct 22, 2012, 2:46:14 PM10/22/12
to nod...@googlegroups.com
Hi All,

Firstly apologies for some of the terminology here, i am coming from an MS stack into Node so some of my language is still stuck there. I am just getting into node, and improving my javascript along the way, and all the community driven stuff is really great.

I am using Mocha and should for my unit test

One issue i am having is mocking the class which deals with the data connections to the database.  I currently have an Account data access object that deals with accounts in the database, this requires a Database object which deals with the link to the database and running the queries.  What i would like to do is mock the Database requires when testing the Account object in isolation.

i tried to use the node-sandboxed-module from Felix Geisendorfer, but i am not sure if i was using it incorrectly, as it sandboxed the object perfectly but then all my should asserts would stop working???.  I also took a look at his other module Gently, but i am gonna be honest i didn't fully understand its usage in my scenario, probably me being dim.

Are there any good examples or best practices for this sort of thing?

Thanks,

Rob 


greelgorke

unread,
Oct 22, 2012, 3:42:41 PM10/22/12
to nod...@googlegroups.com
try sinonjs it works perfectly for me. http://sinonjs.org/

Modika

unread,
Oct 23, 2012, 4:43:08 AM10/23/12
to nod...@googlegroups.com
Hi Greelgorke,

Thanks for you response, i remember seeing it before, but again i was unsure of its usage with actually altering a require, i will check it out again and see whats what.

Rob

greelgorke

unread,
Oct 23, 2012, 5:15:12 AM10/23/12
to nod...@googlegroups.com
its easy. make your object, stub it or some methods of it and use ist. how easy to stub your own objects is another story and depends on your architecture. you should of cause design for testability if your DAO is a singleton (and it should be), than it is easy. but if you create the dao instances in your unit under test, it can be complicated. thats why dependency injection frameworks are exist.
Reply all
Reply to author
Forward
0 new messages