如何为get请求创建单元测试?

时间:2015-12-03 15:54:23

标签: javascript node.js unit-testing karma-runner

作为单元测试的全新手,我非常感谢您的帮助。我一直在寻找示例,但没有成功实现任何测试我的API。

在节点项目中,我首先按照this教程安装了业力。然后我尝试包含相关的包,以尝试使用mocha,chai和sinon实现单元测试。

成功运行一些虚拟测试后,我想实现一个get请求测试,该测试返回一个对象数组:

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"
}
...
]

测试定义如下:

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();
        });
    });
});

结果如下:

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)

因为它总是返回

  

AssertionError:预期0到99

我猜测试没有正确定义。如果有人建议正确实现API调用的测试方法,我将非常感激。

1 个答案:

答案 0 :(得分:0)

我认为这里发生的是:您的测试用例通过触发ajax请求来启动搜索,但由于此请求是异步的,因此javascript继续执行expect(dashboard.count).to.equal(100);。那时你的请求没有完成,计数仍然是0.你可能想要使用这里描述的承诺https://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/或你的搜索中的回调。