Node.JS testing with Jasmine, databases, and pre-existing code

Posted by Jim Rubenstein on Programmers See other posts from Programmers or by Jim Rubenstein
Published on 2012-02-03T01:07:58Z Indexed on 2012/04/03 5:39 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

I've recently built the start of a core system which is likely going turn into a monster product. I'm building the system with node.js, and decided after I got a small base built, that It'd be a great idea to start using some sort of automated test suite to test the application.

I decided to use jasmine, as it seems pretty solid and has a lot of features for stubbing spying and mocking methods and classes. The application has a lot of external data stores and api access (kestrel, mysql, mongodb, facebook, and more).

My issue is, I've got a good amount of code written that I want to start testing - as it represents the underpinnings of the application. What are the best practices for testing methods/classes that access external APIs that I may or may not have control over?

As an example, I have a data structure that fetches a bunch of data from a MySQL database. I want to test the method that retrieves the data; and I'm not sure how to go about it. I could test the fetch method which is supposed to return an array of objects, but to isolate the method from the database, I need to define my own fixture data. So what I end up doing is stubbing the mysql execution, and returning a static dataset. So, I end up writing a function that returns the dataset that makes my test pass. That doesn't seem to actually test the code, other than verifying a method is being called.

I know this is kind of abstract and vague, it seems that the idea of testing is very much abstract though, so hopefully someone has some experience and can guide me in the right direction.

Any advice, or reading I can do is more than welcomed.

Thanks in advance.

© Programmers or respective owner

Related posts about testing

Related posts about TDD