Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 如何在组件集成测试中将测试模型成功添加到存储中_Ember.js - Fatal编程技术网

Ember.js 如何在组件集成测试中将测试模型成功添加到存储中

Ember.js 如何在组件集成测试中将测试模型成功添加到存储中,ember.js,Ember.js,根据这个答案,我有一个组件使用store:Ember.inject.service()。当用户点击提交在商店中创建的新记录时-例如: var address = { line1: this.get('line1'), line2: this.get('line2'), line3: this.get('line3'), town: this.get('town'), county: this.get('county'), postCode: this.get('postC

根据这个答案,我有一个组件使用
store:Ember.inject.service()
。当用户点击提交在商店中创建的新记录时-例如:

var address = {
  line1: this.get('line1'),
  line2: this.get('line2'),
  line3: this.get('line3'),
  town: this.get('town'),
  county: this.get('county'),
  postCode: this.get('postCode'),
  account: account
};

var record = this.get('store').createRecord('address', address);

record.save().then(() => { ... });
现在我想为这个组件编写集成测试,我填写字段,然后点击submit按钮,这一切都很好,但是我如何检查添加到存储的地址

我看到有人提到

var container = this.container;
var store = container.lookup('service:store') || container.lookup('store:main');
但我没能想出最后一步来给我一个有用的断言

更新
我的一些组件在
record.save().then()
中触发了一个操作,因此我可以在测试中使用正常的
this.on('assertMyAction',()=>{…})
订阅该操作。但是,我的一些组件不需要重新启动操作,因此我需要一种不同的方法来了解store.record.save已填充,然后再检查记录上的一些更精细的属性。

嗯,当您调用
createRecord
时,记录已添加到存储中。我的意思是应该将其添加到实时数组中,但属性
isNew
设置为
true

如果要检查记录是否已成功保存,请使用以下方法:

let store = lookup('store:main');
assert.equal(store.all('user').get('length'), 1);

store.find('user', userId).then((user) => {
  assert.equal(user.get('isDirty'), false);
  assert.equal(user.get('isNew'), false);
});
保存前,应检查
isNew
是否等于
true
。保存后,您还可以检查属性,如
isValid
isError
。您可以找到更多潜在有用的属性

余烬2.0
您需要使用
this.container.lookup('service:store')
而不是
store:main

嗯,当您调用createRecord时,记录被添加到了存储中。你想检查它是否成功保存吗?我想检查两者,检查它是否被添加,还检查它是否成功。谢谢!我的一些组件在记录保存的
中触发一个动作,然后
。所以我在测试中订阅了它,并检查isNew是否为false等。但是,我的一些组件不会触发这样的操作,因此我可以检查长度是否为1,如
store.all('user').get('length')
,是否有方法等待记录完成保存?因为此时
user.get('isNew'))
对于这些组件返回真值,一个简单的
设置超时(()=>{…})
并没有切断它。在我的测试中
Ember.run。稍后
完成了任务,但没有记录。我认为您必须为您的测试公开一些承诺或挂钩,然后显式地订阅这些方法。若在触发请求之前有记录实例,那个么可以尝试使用模型的didLoad、didUpdate事件。我无法处理这些事件,最后我向其余组件添加了可选操作,然后订阅了测试中的组件,但我觉得必须有更好的方法