简体   繁体   English

使用Jasmine和DS.FixtureAdapter测试Ember-data

[英]Testing Ember-data w/Jasmine and DS.FixtureAdapter

I'm trying to do a Jasmine test of ember-data (using the current master) using the DS.FixtureAdapter. 我正在尝试使用DS.FixtureAdapter对ember-data进行Jasmine测试(使用当前的master)。 I've tried dozens of variations on the below code (with and without trying to create an Application namespace). 我已经尝试了以下代码的几十种变体(有和没有尝试创建一个Application命名空间)。 I've also gone into the ember-data source to try and see what's going on, as well as referenced the tests in ember-data itself as an example. 我也进入了ember-data源,试着看看发生了什么,并以ember-data本身的测试为例。

I've also tried variations of Person.find(1), using Ember.run blocks and Jasmine wait()'s. 我也尝试过使用Ember.run块和Jasmine wait()的变体Person.find(1)。

Whatever I try, store.find(Person, 'test') returns a result but attempting to get one of the attributes results in null (test assertion fails). 无论我尝试什么, store.find(Person, 'test')返回一个结果,但是尝试获取其中一个属性会导致null(测试断言失败)。 What is it I'm not seeing? 我没看到什么? Thanks for any help! 谢谢你的帮助!

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

Whatever I try, store.find(Person, 'test') returns a result but attempting to get one of the attributes results in null (test assertion fails). 无论我尝试什么,store.find(Person,'test')都会返回一个结果,但是尝试获取其中一个属性会导致null(测试断言失败)。 What is it I'm not seeing? 我没看到什么? Thanks for any help! 谢谢你的帮助!

This is a timing issue. 这是一个时间问题。 When you call store.find() it runs query asynchronously and returns a model promise. 当您调用store.find()它会异步运行查询并返回模型承诺。 That means the query is still running (or scheduled to run) when control returns to your test, resulting in a failed expectation. 这意味着当控制返回到测试时,查询仍在运行(或计划运行),从而导致预期失败。

This is what we love about ember, it means your app can treat kyle as if the data were present and trust that values will be updated automagically via bindings when the data becomes available. 这就是我们对ember的喜爱,这意味着你的应用程序可以将kyle视为数据存在,并相信当数据可用时,值将通过绑定自动更新。

Of course all this magic is not so great when it is preventing your test from passing. 当然,当它阻止你的测试通过时,所有这些魔法并不是那么好。 Here are some alternative approaches: 以下是一些替代方法:

1) Register a didLoad callback 1)注册didLoad回调

kyle = store.find(Person, 'test');
kyle.on('didLoad', function() {
  console.log('should = kyle: ', Em.get(kyle, 'firstName'));
});

2) Instead of didLoad could use more blackbox testing approach and just verify that the name is set propertly within 100 ms of having called find - of course this can lead to brittle tests Ember.run.later(this, function() { console.log('should = kyle: ', Em.get(kyle, 'firstName')); console.log('should = kim: ', Em.get(App.kim, 'firstName')); }, 100); 2)而不是didLoad可以使用更多的黑盒测试方法,只需验证名称是否在调用find的100毫秒内设置属性 - 当然这可能会导致脆弱的测试Ember.run.later(this,function(){console。 log('should = kyle:',Em.get(kyle,'firstName')); console.log('should = kim:',Em.get(App.kim,'firstName'));},100) ;

I believe that in a jasmine test you could wrap your setup code in a runs() method and use waitsFor to verify that the value has been set as expected: 我相信在jasmine测试中,您可以将您的设置代码包装在runs()方法中,并使用waitsFor来验证该值是否已按预期设置:

  waitsFor(function() {
    return Em.get(kyle, 'firstName') == 'Kyle';
  }, "x to be set to 5", 100);

See this JSBIN for working (non-jasmine) example: http://jsbin.com/apurac/4/edit 有关工作(非茉莉花)示例,请参阅此JSBIN: http ://jsbin.com/apurac/4/edit

See this post for tips on async testing with jasmine: http://blog.caplin.com/2012/01/17/testing-asynchronous-javascript-with-jasmine/ 有关使用jasmine进行异步测试的提示,请参阅此文章: http//blog.caplin.com/2012/01/17/testing-asynchronous-javascript-with-jasmine/

Also, be sure to set Ember.testing = true for all of your tests. 此外,请确保为所有测试设置Ember.testing = true See this SO post for detail: Is it recommended to set Ember.testing = true for unit tests? 有关详细信息,请参阅此SO帖子: 是否建议为单元测试设置Ember.testing = true?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM