Hey Martin,

The path of least resistance is often to just to use the database. 
Generally, you'll set up a second "test" db so that you don't clobber the 
data you're developing with. Before your test, you seed the db with some 
simple data. In your test you let your code pull that data out and check 
that it did it correctly.

Now, this solution has a small speed penalty as opposed to something that 
mocks the db connection (especially since you'll need to reset the state of 
the db before every test), and you won't have isolated your code quite as 
much as some purists would prefer. But it tends to be easy to write tests 
this way, and the tradeoffs are often acceptable.

Most of this is based on my experience in other web frameworks. I've been 
taking this route with Node.js, and it's been smooth so far, but I'm 
curious to see if the Node community develops its own set of preferences on 
this matter.

Ian
 
On Saturday, March 10, 2012 3:31:21 PM UTC-8, Martin Lundberg wrote:
>
> I've got an object named UserRepository which has a method named find 
> which uses node-mysql to fetch rows from the database and I'm confused on 
> how to test this code. I've tried reading about mocks but I still can't 
> figure out exactly how to test the code. How do you  test code which talks 
> to a database?
>
> I've also read "only mock what you own" and since I don't own node-mysql 
> client I'm not sure what to do.
>
> Would really appreciate examples.
>
> Thanks,
>
> -Martin
>

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