使用Mocha进行Ajax测试-Node.js [英] Ajax testing with Mocha - Nodejs
本文介绍了使用Mocha进行Ajax测试-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用Mocha为我的一个应用程序编写测试. 重新加载页面后,我可以运行不同的场景.
I tried using Mocha to write tests for one of my application. I can able to run different scenarios when page reloads.
考虑以下情况
1)用户在文本字段用户名"中输入值
2)单击保存"按钮
3)这将发送ajax调用以保存用户名,并且响应可能会在1-2秒内返回
4)收到回复后会显示一条消息
如何针对上述情况实施测试?
How can i implement test for above scenario?
注意:我尝试了以下代码,但浏览器根本没有等待
NOTE : I tried the below code, but the browser isn't waiting at all
describe("Ajax test", function(){
before(function(){
return this.browser.field('save').click()
})
it("Tests save username", function(){
this.browser.wait(6000, function() {
//code to check if success message is shown in body
})
})
})
谢谢, 巴兰
推荐答案
在每个测试用例中都可以进行回调,因此在进行此回调之前,您应该等待服务器的响应.
There is a callback you can make in each test case, and you should wait for a response from the server before making this callback.
模式应该是这样的:
// Test case. Notice the `done` param
it('Should make an async call', function(done){
// Make an async call, with a callback function
async_call(function(){
// Do whatever you need (test some values)
// Then call done
done();
});
});
这篇关于使用Mocha进行Ajax测试-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文