Mock/Test Mongodb Database Node.js

纵饮孤独 提交于 2019-12-03 10:45:25

问题


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


回答1:


There are some

http://mongodb.github.com/node-mongodb-native/github/github.html#test-helpers-and-libraries

but you might want to just search on github or ask in the google group as well.




回答2:


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:

  • sinon
  • rewire
  • horaa
  • sandboxed-module

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?




回答3:


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



来源:https://stackoverflow.com/questions/10378116/mock-test-mongodb-database-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!