lundi 5 octobre 2015

How to inject mocked object in node.js?

I am mainly a Java guy and still learning how to develop REST API using Node.js and express framework. In my REST API, I have divided code in 3 layers. service layer, business layer and DAO layer.

To unit test my service layer, I have written code using chai and mocha frameworks. I would like to know how to mock DAO layer and pass mocked DAO object to business layer.

In Java world it was simple using easy mock and power mock frameworks but I am having difficult time in finding out how same can be done in node.js world.

I have written following code but it is not working. Can anyone please help me understand what am I missing here?

it("Should return entity with its unique ID when valid entity name is provided", function(done){

    var mock = sinon.mock(entityDao);
    mock.expects('createEntities').withArgs(sinon.match.any).returns(true);

    var expectedStatusCode = 201;
        var entity = {
            'name' : 'Sprinkler 1',
            'city' : 'Pune',
            'state' : 'Maharashtra',
            'country' : 'India' 
        };
        var requestBody = {
            'entities' : [entity]
        };
        server
            .post(entityApiPath)
            .set('Content-Type', 'application/json')
            .set('Accept', 'application/json')
            .send(requestBody)
            .expect(expectedStatusCode)
            .expect('Content-Type', /json/)
            .end(function(error, response) {
                var responseToCreate = response.body;
                console.dir(responseToCreate);
                expect(responseToCreate).not.to.be.null;
                responseToCreate.should.have.property('createdEntities');
                responseToCreate.should.have.property('failedEntities');
                done();
});

Aucun commentaire:

Enregistrer un commentaire