mocha-phantomjs回调函数的测试用例 [英] mocha-phantomjs test case for callback function

查看:177
本文介绍了mocha-phantomjs回调函数的测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块,我有这个功能

I have a module in which I have this function

flickrPhotoSearch: function (searchByName, flickrUserKey, numberOfImages, callbackData) {
    return $.ajax({
        url: commonConstants.BASE_URL + "flickr.photos.search&api_key=" + flickrUserKey + "&tags=" + searchByName + "&format=json&jsoncallback=?",
        dataType: 'json',
        async: true,
        success: function (jsonData) {
            if (jsonData.stat === commonConstants.JSON_SUCCESS) {
                if (jsonData.photos['photo'].length < commonConstants.DATA_LENGTH) {
                    callbackData(jsonData);
                } else {
                    var flickrImage = flickrApplication.jsonToImage(jsonData, numberOfImages);
                    callbackData(flickrImage);
                }
            } else {
                callbackData(jsonData);
            }
        }
    });
}

我想测试这个函数,我选择 mocha -phantomjs 。这是我的测试用例

I want to test this function and I choose mocha-phantomjs for it. And this is my test case

describe("flickrphotoSearch", function () {
    it("should fail with wrong key", function () {
        flickrApplication.flickrPhotoSearch(testConstant.CORRECT_NAME, testConstant.WRONG_KEY, testConstant.CONSTANT_ONE, handleData);
        function handleData (photoUrl) {
            assert.equals(photourl.stat, "pass", photoUrl.message);
        }
    });
});

现在,此测试应通过给出错误无效的API密钥 code>。但它过去了。我认为这是因为我使用断言内部回调函数即 handleData()

Now this test should fail by giving error "Invalid API Key". But It got passed. I think this is because I used assertion inside callback function i.e. handleData().

我使用 mocha-phantomjs 设置和 chai 断言库。

I am using mocha-phantomjs setup and chai assertion library.

我搜索教程和演示,但没有找到任何。我也试过了 mocha-phantomjs 例子,但没有帮助我在这里发布。

I searched for tutorials and demos but coudn't find any. Also I tried mocha-phantomjs examples but with no help I am posting here.

请告诉我如何测试回调函数在 mocha-phantomjs

Please tell me how to test callback function in mocha-phantomjs.

推荐答案

异步但正在同步测试的测试的典型症状。解决方案是在测试中使用 done 回调:

What you describe is the typical symptom for a test that is asynchronous but is being tested synchronously. The solution is to use the done callback in your test:

it("should fail with wrong key", function (done) {
    flickrApplication.flickrPhotoSearch(testConstant.CORRECT_NAME, testConstant.WRONG_KEY, testConstant.CONSTANT_ONE, handleData);
    function handleData (photoUrl) {
        assert.equals(photourl.stat, "pass", photoUrl.message);
        done();
    }
});

当您将 done 回调你给 it ,你告诉Mocha测试是异步的,然后你必须在异步回调中调用它( handleData

When you add the done argument to the callback you give to it, you tell Mocha that the test is asynchronous and then you must call it in your asynchronous callback (handleData here) to tell Mocha that the test is over.

否则,Mocha将运行给予的回调并且不会等待 handleData 执行。测试将立即结束,没有错误,所以摩卡会说它已过。

Otherwise, Mocha will run the callback given to it and won't wait for handleData to execute. The test will end right away, without errors, so Mocha will say it has passed.

这篇关于mocha-phantomjs回调函数的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆