Reputation: 4568
I am working on node js rest api in which database is Postgres and we are not using any ORM. How I am writing is as below which is hitting database for create and update
it('it should create customer', (done) => {
const payload = {
customerId: "test",
customerName: "test",
customerAddress: "Some place, NWZ"
}
chai
.request(server)
.post('/customer')
.send(payload)
.end((err, res) => {
res.should.have.status(200);
res.body.success.should.equal(true);
done();
});
});
Now I want to know that what is best way to write unit test cases ? Like
Or in any way we can mock database ? What is best way to do it ?
Upvotes: 0
Views: 1007
Reputation: 476
There is some debate regarding whether unit tests should hit database. But in general, I would say hitting a real database in testing should be categories as integration test, not unit test.
So for your question 2: I would say: no, you should not write unit test case that hit database.
Then for your question 1: yes, you should mocking api response, the library you could choose, e.g. sinon.
---- Updated ----
There is an article regarding the topic, if you are interested, please refer to the following: Hitting Real Database? Unit Test and Integration Test for RESTful API in Nodejs Environment
Upvotes: 1