我正在尝试使用DS.FixtureAdapter对ember-data (使用当前的主文件)进行一次Jasmine测试。我已经在下面的代码上尝试了几十种变体(使用和不尝试创建应用程序命名空间)。我还进入了ember-data源,尝试查看发生了什么,并引用了ember-data本身中的测试作为示例。
我还使用Ember.run块和Jasmine wait()尝试了Person.find(1)的变体。
无论我如何尝试,store.find(Person, 'test')都会返回一个结果,但是尝试获取其中一个属性会导致null (测试断言失败)。有什么是我看不到的?谢谢你的帮助!
describe "a test", ->
store = null
Person = null
beforeEach ->
store = DS.Store.create
revision: 11
adapter: 'DS.FixtureAdapter'
Person = DS.Model.extend
firstName: DS.attr('string')
lastName: DS.attr('string')
age: DS.attr('number')
it "works or does it", ->
Person.FIXTURES = [{
id: 'test'
firstName: 'Kyle'
lastName: 'Stevens'
age: 30
}]
kyle = store.find(Person, 'test')
expect(Em.get(kyle, 'firstName')).toEqual('Kyle')发布于 2013-01-20 15:34:45
不管我怎么尝试,store.find(Person,‘
’)都会返回一个结果,但是尝试获取其中一个属性的结果是null (测试断言失败)。有什么是我看不到的?谢谢你的帮助!
这是一个时机问题。当您调用store.find()时,它会异步运行查询并返回一个模型承诺。这意味着当控制返回到您的测试时,查询仍在运行(或计划运行),从而导致预期失败。
这就是我们喜欢ember的地方,这意味着你的应用程序可以像对待数据一样对待kyle,并相信当数据可用时,值将通过绑定自动更新。
当然,当它阻止你的测试通过时,所有这些魔法都不是那么伟大。以下是一些替代方法:
1)注册didLoad回调
kyle = store.find(Person, 'test');
kyle.on('didLoad', function() {
console.log('should = kyle: ', Em.get(kyle, 'firstName'));
});2)代替didLoad,可以使用更多的黑盒测试方法,只需验证名称是否在调用find后的100ms内设置正确-这当然会导致脆性测试函数(Ember.run.later,console.log(‘didLoad= kyle:',Em.get(kyle,'firstName'));console.log(’console.log= kim:',Em.get(App.kim,'firstName'));},100);
我相信在jasmine测试中,您可以将设置代码包装在runs()方法中,并使用waitsFor验证该值是否已按预期设置:
waitsFor(function() {
return Em.get(kyle, 'firstName') == 'Kyle';
}, "x to be set to 5", 100);有关工作(非jasmine)示例,请参阅此JSBIN:http://jsbin.com/apurac/4/edit
有关使用jasmine进行异步测试的技巧,请参阅此帖子:http://blog.caplin.com/2012/01/17/testing-asynchronous-javascript-with-jasmine/
此外,请确保为所有测试设置Ember.testing = true。详情请参阅这篇SO post:Is it recommended to set Ember.testing = true for unit tests?
https://stackoverflow.com/questions/14420313
复制相似问题