测试模特商店的最佳方法是什么?我正在使用ember-data 2.7.0并且想测试我是否可以创建模型实例并将其成功保存到后端(firebase)。
我已将var record = store.create
和record.save
包含在Ember.run函数中,但我得到了You can only unload a record which is not inFlight. `<(subclass of DS.Model):ember227:null>
答案 0 :(得分:2)
有很多方法可以测试这个,但我更喜欢的方法是使用ember-sinon进行间谍/抄袭。
假设您有创建和保存记录的操作:
import Route from 'ember-route';
export default Route.extend({
actions: {
createAndSaveTheRecord() {
this.store.createRecord('dummy_model', {
id: 'dummy',
name: 'dummy'
}).save();
}
}
});
您可以进行如下测试:
import sinon from 'sinon';
test('should create a record', function(assert) {
assert.expect(1);
// Arrange
let stub = sinon.stub().returns({save: sinon.stub()});
let route = this.subject({store: {createRecord: stub}});
// Act
route.send('createAndSaveTheRecord');
// Assert
assert.ok(stub.calledWith('dummy_model', {id: 'dummy', name: 'dummy'}));
});
test('should save the created record', function(assert) {
assert.expect(1);
// Arrange
let spy = sinon.spy();
let route = this.subject({
store: {
createRecord: sinon.stub().returns({
save: spy
})
}
});
// Act
route.send('createAndSaveTheRecord');
// Assert
assert.ok(spy.calledOnce);
});