jeudi 3 décembre 2015

How to create unit testing for get requests?

As a complete newbie to unit testing I would really appreciate your help. I've been looking around for examples but haven't managed to successfully implement any to test my APIs.

In the node project I've first installed karma by following this tutorial. Then I've tried to include relevant packages in order to try to implement unit testing using mocha, chai and sinon.

After successfully running some dummy test, I wanted to implement a test for get request which returns an array of objects:

url: http://localhost:3000/api/searchNutrients?keywords=beef

[
{
id: "07921",
friendly_name: "Bacon and beef sticks"
},
{
id: "07001",
friendly_name: "Barbecue loaf, pork, beef"
},
{
id: "16007",
friendly_name: "Beans, baked, canned, with beef"
},
{
id: "13330",
friendly_name: "beef"
},
{
id: "0",
friendly_name: "beef bouillon"
},
{
id: "06981",
friendly_name: "beef bouillon cube"
},
{
id: "0",
friendly_name: "beef bouillon cubes"
},
{
id: "06032",
friendly_name: "beef bouillon granules"
}
...
]

The test is defined as follows:

var expect = require("chai").expect;

var dashboard = {
    count: 0,
    status: '',
    keywords: '',
    searchNutrients: function(keywords) {
        var self = this;
        $.ajax({
            url: 'http://localhost:3000/api/searchNutrients?keywords=' + keywords,
            dataType: 'json',
            success: function (data) {
                console.log('data: ' + JSON.stringify(data));
                self.count = parseInt(data.count);
            }
        });
    }
};

describe('Dashboard API', function(){
    describe("#searchNutrients", function(){
        beforeEach(function() {
            sinon.spy($, 'ajax');
        });

        afterEach(function() {
            $.ajax.restore();
        });

        it('should return some results', function(done) {
            dashboard.searchNutrients('beef', sinon.spy());
            expect(dashboard.count).to.equal(100);

            done();
        });
    });
});

and results is the following:

INFO [watcher]: Changed file "/home/niko/workspace/meel/test/app_api/routes/dashboard.test.js".
INFO [framework.browserify]: 201903 bytes written (0.03 seconds)
INFO [framework.browserify]: bundle updated
INFO [watcher]: Changed file "/tmp/fa9aa31d8fe449581332a3a0091ec66b.browserify".
PhantomJS 1.9.8 (Linux 0.0.0) Dashboard API #searchNutrients should return some results FAILED
        AssertionError: expected 0 to equal 99
            at absolute/tmp/fa9aa31d8fe449581332a3a0091ec66b.browserify?90c188375614d4d087adf945e50e05cac4eae7e2:210
            at assertEqual (absolute/tmp/fa9aa31d8fe449581332a3a0091ec66b.browserify?90c188375614d4d087adf945e50e05cac4eae7e2:765)
            at absolute/tmp/fa9aa31d8fe449581332a3a0091ec66b.browserify?90c188375614d4d087adf945e50e05cac4eae7e2:3945
            at absolute/tmp/fa9aa31d8fe449581332a3a0091ec66b.browserify?90c188375614d4d087adf945e50e05cac4eae7e2:7712
PhantomJS 1.9.8 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.024 secs / 0.007 secs)

As it always returns

AssertionError: expected 0 to equal 99

I guess the test is not defined correctly. I would be really thankful if anyone suggested the way to correctly implement the test for API calls.

Aucun commentaire:

Enregistrer un commentaire