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
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?
In order to spin up a real in-memory mongodb for testing, https://github.com/nodkz/mongodb-memory-server helps as well.
Related
If use Ruby on Rails framework, we can run the test in a easy way. It can create a test database, and we can make some fake data. After all the test finished, the test database will be cleaned.
Now want to do the same test with Node.js, Express framework and ElasticSearch. If create index data in the real ES server, maybe not the good way(Think ES as development environment used). Is there a better way to do it as RoR above?
I have researched from internet, but didn't find good example.
I want to make some test for the database on my nodejs app. I want to know the pros and cons of using mocks or SqLite to test the database.
I found this question:
Should I use an In-Memory Database instead of mocking out my Repositories?
But it didn't give much information about each of them.
For me it depends. If I'm using an ORM I will use SQLite - I assume that the ORM stuff works as expected and it's easier than mocking the ORM.
If I'm writing my own code to do database stuff, I'll mock it. If I have a database service layer, I'll mock it.
Basically, if I write it I'll mock it and if I use an ORM or library I'll use SQLite.
I am new on developing in Node.js, I would like to know if there's a way to have ElasticSearch embedded in a Node.js application for testing purposes or if there's another way to test interaction with ElasticSearch without having an ElasticSearch running instance (mock ?)
There are no existing mock libraries for Elasticsearch and Node at the moment. If you know exactly what queries you are going to be performing, you could use a mocking library to mock the entire ES client and assert simple behaviors.
What I'd recommend, however, is actually running ES in a development environment for testing. I find that mocking external services is a pretty tricky thing to do in general, and your tests will likely be much more robust / trustworthy if running against an actual instance of the server.
I'm using jenkins to automate the testing of my node js application? How do I test the queries to the database? How does Jenkins know to build the mongo db database first? How does automated testing work with database?
I don't use Jenkins for CI, I tend to use TeamCity, but I think your questions can be answered independently of which CI technology you use.
There is no definite answer to your questions, it all depends:
If you are looking to implement full integration tests, including querying a real database, then I would suggest creating a separate database just for testing like - mydb-test. You would have to configure your tests to use this database via config etc. In case you want to isolate your tests from the data access layer, then you need to mock the data access. This can be done by using a mock library, this will be easier to use depending on how well architected is your application code, where dependency injection IMHO is really important on this matter.
I'm currently building a rest api and I'm struggling to find the best way to unit test each route.
A route handler performs various things and one of them is to execute a query to mongodb. I can unit test the route handler by using stubs, but if I'm testing the query I cannot stub the query itself, I need to have an in-memory mongodb that I could reset and insert new data for each test.
How do you test queries? I'm thinking that the only real way to ensure that the query does what I need is to use a real mongodb database installed in the testing machine (typically in the same machine used for developing).
yes, just as for relation databases, you need to have real base. if mongo offers in-memory auto-created version then it's easy. if not, then each developer has to have running mongo before he runs integration tests. for CI you can have one single dedicated mongo but than you have to prevent concurrent access (schema creation, multiple transactions etc). you should also implement automatic creation of schema if needed and empty database before each test. in relational db rollback is usually enough. when it's not enough then trimming all tables helps. although we had to implement it manually as we couldn't find any existing tools