28

I am learning nodejs and I have a mongodb database with which i have to interact with. I am currently thinking of using mocha for a unit test framework and zombie.js for a acceptance test framework. I was wondering how could I do full scale acceptance tests which hit the mongodb database. Is there a framework/module that helps with replacing the database with a test database or does either mocha or zombie.js have functionality that can easily be used to replace the database.

Also is there a framework that is similar to the idea of factories (instead of fixtures) in creating database objects.

A similar concept that I have encountered in the rails world is in rspec, there is a spec_helper.rb file which runs before the tests are run which set the projects configuration to decide which database to hit when running tests. And it uses database_cleaner to clean out the test database before tests are run. For factories, i have used Factory girl to create factory objects from database schema again in the rails world.

Thanks

dom
  • 652
  • 1
  • 16
  • 35
GTDev
  • 5,488
  • 9
  • 49
  • 84
  • I've created a library to allow factory_girl/machinist behaviour - ectypes (strange word, but it makes sense when you know what it means). It's a bit different to the Ruby world as there's no standard ORM/ODM in Node.js, so you have to define a 'strategy' for the persistence layer. They're not hard to make. I have one for Sequelize at the moment. See https://github.com/nicholasf/ectypes.js – nicholasf Aug 20 '12 at 09:50

2 Answers2

3

If your database access is a separate module, you can mock out the module or parts of the module (e.g., the configuration part) using one of the following:

The answers to the following related question lists a number of possible solutions/approaches: How do you mock MySQL (without an ORM) in Node.js?

Community
  • 1
  • 1
dule
  • 17,798
  • 4
  • 39
  • 38
3

In order to spin up a real in-memory mongodb for testing, https://github.com/nodkz/mongodb-memory-server helps as well.

udo
  • 1,486
  • 13
  • 18