我想测试返回有关数据集,我通过AJAX加载的见解一个原型方法。
$.getJSON('../data/bryce.json').done(function(data) {
insights = new Insights(data);
describe("People Method", function() {
console.log('it executes this far');
it("should return complete people data", function() {
console.log('but not this far');
expect(insights.people()).toBeTruthy();
});
});
});
当我运行这个测试套件,描述()执行,而不是它()。 我是很新,在一般的JavaScript测试,所以我想我做错了。 但我不知道它是什么。
另外,因为我正在使用的数据是一个巨大的 JSON文件,它不是真的有可能包括在此文件。 它也有可能甚至提供样本大小的版本。 数据集中的每个对象都是数百行的。
茉莉的作品掀起了排队机制并执行所有的describe
和it
功能的排队工作将被执行。
在茉莉花asyncronously做的工作需要你遵循一定的模式。
茉莉花的1.x
describe('some suite', function(){
it('some test', function(){
var data;
//Execute some async operation
runs(function(){
$.get('myurl').done(function(d){ data = d; });
});
//Wait for it to finish
waitsFor(function(){
return typeof data !== 'undefined';
});
//Assert once finished
runs(function(){
expect(data.foo).toBe('bar');
});
});
});
茉莉花1.x中使用一种特殊的轮询机制,以保持轮询waitsFor
方法,直到超时,或返回true,然后执行最终的runs
方法。
茉莉花2.X
describe('some suite', function(){
var data;
beforeEach(function(done){
$.get('myurl').done(function(d){
data = d;
//Signal test to start
done();
});
});
it('some test', function(done){
expect(data.foo).toBe('bar');
//Signal test is finished
done();
});
});
茉莉2.X是有点不同的,因为它使用的信令机制来指示何时开始和完成测试。 您的规格可以在一个可选的done
方法,使用同步你的测试。
如果您使用done
的方法beforeEach
然后直到调用该方法不会启动测试。
如果您使用done
方法在it
的功能,则测试将无法完成,直到该方法被调用。
这两种可用于你的测试中有效地管理异步行为。