0
我刚刚开始使用茉莉花单元测试,并且遇到了一些测试异步调用的麻烦。茉莉花异步调用问题
我有一个ajax调用,我试图测试,我试着在控制台中,所以我知道它的工作原理是如何工作的。我想我正在测试我在控制台中做的同样的事情,但可能是错误的。
这里是控制台:
> mg = new MandellMVC()
MandellMVC {getSessionId: function, setSessionId: function, isValidGetFunction: function, getURLPrefix: function, setURLPrefix: function…}
> mg.setUseLocalData(true);
true
> var log = new Log(73936780)
undefined
> log.setLogType('Proc')
true
> log.fetch(mg, function(){console.log('done');})
true
done
设置本地数据只是发送从本地文件的HTTP请求到外部服务器,或加载数据之间变化。
茉莉测试代码是在这里:
describe("Log Model", function() {
var mg = new MandellMVC();
mg.setUseLocalData(true);
var log;
beforeEach(function() {
log = new Log(73936780);
});
describe("function fetch", function() {
it("returns false if log type is invalid", function() {
expect(log.fetch(mg, function(){})).toBeFalsy();
});
// Not sure why this needs to be here too?
log = new Log(73936780);
log.setLogType('Proc');
it("should make a real ajax request", function() {
var callback = jasmine.createSpy();
log.fetch(mg, callback);
waitsFor(function() {
return callback.callCount > 0;
});
runs(function() {
expect(callback).toHaveBeenCalled();
});
});
});
});
第一测试通过。但后来第二个给出了错误timeout: timed out after 5000 msec waiting for something to happen
。我试图按照教程,但显然不是很好。
谢谢你,任何帮助,非常感谢!
什么开发人员工具(或萤火虫)说关于AJAX请求?它成功了吗?另外,你应该在'describe'内部移动'beforeEach',因为它只适用于一个级别(参见[docs](http://pivotal.github.io/jasmine/#section-Setup_and_Teardown)) – scriptin 2013-05-14 21:29:26
'beforeEach'和'afterEach'级联到嵌套描述就好了。请参阅您关联的文档中的下一部分 – Gregg 2014-02-08 22:07:42