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

How to create unit testing for get requests?

本文关键字:创建 单元测试 请求 get      更新时间:2023-09-26

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

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

在成功运行了一些伪测试之后,我想实现一个get请求的测试,该请求返回一个对象数组:

网址: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)

因为它总是返回

断言错误:应为0,等于99

我想这个测试的定义不正确。如果有人提出正确实现API调用测试的方法,我将非常感谢。

我认为这里发生的事情如下:您的测试用例通过触发ajax请求来启动搜索,但由于该请求是异步的,javascript将继续执行expect(dashboard.count).to.equal(100);。此时,您的请求尚未完成,计数仍然为0。您可能需要使用此处所述的承诺https://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/或者在搜索中进行回调。