Ember.js 单元测试Ember.Object和DS.Model metaForProperty不一致
我正在为我们的Ember应用程序建立单元测试。我目前针对的一组测试是我们的模型。我有一组测试,对于基于Ember数据的模型来说效果非常好,但是在基于Ember.Object时似乎失败了。以下是两个例子:Ember.js 单元测试Ember.Object和DS.Model metaForProperty不一致,ember.js,ember-data,Ember.js,Ember Data,我正在为我们的Ember应用程序建立单元测试。我目前针对的一组测试是我们的模型。我有一组测试,对于基于Ember数据的模型来说效果非常好,但是在基于Ember.Object时似乎失败了。以下是两个例子: App.Person = DS.Model.extend({ First: DS.attr("string"), Last: DS.attr("string") }); App.Person2 = Ember.Object.extend({ First: null,
App.Person = DS.Model.extend({
First: DS.attr("string"),
Last: DS.attr("string")
});
App.Person2 = Ember.Object.extend({
First: null,
Last: null
});
以及DS.Model通过的测试:
it('has a valid attribute: First', function() {
var property = App.Person.metaForProperty('First');
expect( property.type ).to.eql('string');
expect( property.isAttribute ).to.eql(true);
});
然后,当对Ember.Object使用相同的结构时:
it('has a valid attribute: First', function() {
var property = App.Person2.metaForProperty('First');
});
我得到以下错误:
Error: Assertion Failed: metaForProperty() could not find a computed property with key 'First'.
at new Error (native)
at Error.Ember.Error (http://0.0.0.0:3385/app/js/components/ember/ember.js:844:19)
at Object.Ember.assert (http://0.0.0.0:3385/app/js/components/ember/ember.js:73:11)
at Function.Mixin.create.metaForProperty (http://0.0.0.0:3385/app/js/components/ember/ember.js:13247:11)
at Context.<anonymous> (http://0.0.0.0:3385/tests/model-person-test.js:6:35)
at invoke (http://0.0.0.0:3385/tests/bower_components/ember-mocha-adapter/adapter.js:60:8)
at Context.suite.on.context.it.context.specify.method (http://0.0.0.0:3385/tests/bower_components/ember-mocha-adapter/adapter.js:102:13)
at Test.require.register.Runnable.run (http://0.0.0.0:3385/tests/assets/mocha.js:4200:15)
at Runner.require.register.Runner.runTest (http://0.0.0.0:3385/tests/assets/mocha.js:4591:10)
at http://0.0.0.0:3385/tests/assets/mocha.js:4637:12
错误:断言失败:metaForProperty()找不到键为“First”的计算属性。
新错误时(本机)
在Error.Ember.Error(http://0.0.0.0:3385/app/js/components/ember/ember.js:844:19)
在Object.Ember.assert(http://0.0.0.0:3385/app/js/components/ember/ember.js:73:11)
位于Function.Mixin.create.metaForProperty(http://0.0.0.0:3385/app/js/components/ember/ember.js:13247:11)
在上下文中。(http://0.0.0.0:3385/tests/model-个人测试(js:6:35)
在调用时(http://0.0.0.0:3385/tests/bower_components/ember-摩卡适配器/adapter.js:60:8)
位于Context.suite.on.Context.it.Context.specify.method(http://0.0.0.0:3385/tests/bower_components/ember-摩卡适配器/adapter.js:102:13)
在Test.require.register.Runnable.run(http://0.0.0.0:3385/tests/assets/mocha.js:4200:15)
在Runner.require.register.Runner.runTest处(http://0.0.0.0:3385/tests/assets/mocha.js:4591:10)
在http://0.0.0.0:3385/tests/assets/mocha.js:4637:12
有人能提供关于可能出现问题的见解吗?是的,
第一个
/最后一个
不是人的计算属性2
,它们只是属性
这会管用的
App.Person2 = Ember.Object.extend({
First: null,
Last: null,
Blah: function(){
}.property('First')
});
var j = App.Person2.metaForProperty('Blah');
console.log(j);
当您执行DS.attr()操作时,Ember数据实际上是在那里注入一个计算属性,请参见:这里有一组更新的断言,对我有用。它看起来像是因为它只使用了Ember.Object,而DS.Model的额外好处根本不存在
it('has a valid attribute: First', function() {
var person = App.Person.create({
id: 1,
First: 'Andy'
});
expect( App.Person.proto().hasOwnProperty('First') ).to.eql(true);
expect(typeof person.get('First')).to.eql('string');
expect(person.get('First')).to.eql('Andy');
});