用Mocha – Nodejs进行Ajaxtesting

我尝试使用摩卡来写我的应用程序的testing。 当页面重新加载时,我可以运行不同的场景。

考虑下面的情况

1)用户在文本字段“用户名”中input值

2)点击“保存”button

3)这将发送一个Ajax调用来保存用户名和响应可能会在1到2秒内回来

4)接收到响应后显示一条消息

我怎样才能实现以上情况的testing?

注意:我尝试了下面的代码,但是浏览器并没有等待

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

谢谢,巴兰

在每个testing用例中都可以进行callback,在进行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(); }); });