如何在测试 setupController 或私有 Route 方法时模拟模型
How to mock model when testing setupController or private Route methods
当我尝试通过在我的路由(单元)测试中写入 let mockModel = this.owner.lookup('model:realModel');
来模拟 model
时,我收到错误
Error: You should not call 'create' on a model. Instead, call 'store.createRecord' with the attributes you would like to set.
模拟 model
以测试 setupController
和其他私有路由方法中的处理的正确方法是什么?
说明测试setupController/private路由方法的代码:
app\routes\application.js
export default Route.extend({
_postModelProcessing(inputModel){
....
return processedModelData;
},
setupController(controller, model){
let processedModelData = _postModelProcessing(model);
}
})
tests\unit\routes\application-test.js
module('Unit | Route | application', function(hooks) {
setupTest(hooks);
...
test('private _postModelProcessing correctly processes model data', function(assert) {
let route = this.owner.lookup('route:application');
// This line throws the Error mentioned above
let mockModel = this.owner.lookup('model:realModel');
// what I hoped to do, if the above line didn't throw an error:
let mockModelData = [
mockModel.create({
category: 'Leopard',
childCategories: [],
parentCategory: null
}),
mockModel.create({
category: 'Snow Leopard',
childCategories: [],
parentCategory: 'Leopard'
}),
mockModel.create({
category: 'Persian Leopard',
childCategories: [],
parentCategory: 'Leopard'
})
]
let processedData = route._postModelProcessing(mockModelData);
let leopardFirstChild = processedData[0].get('childCategories')[0];
assert.equal(leopardFirstChild.get('category'), 'Snow Leopard');
})
});
正如错误信息所说,使用store.createRecord
:
module('Unit | Route | application', function(hooks) {
setupTest(hooks);
...
test('private _postModelProcessing correctly processes model data', function(assert) {
let route = this.owner.lookup('route:application');
let store = this.owner.lookup('service:store');
// what I hoped to do, if the above line didn't throw an error:
let mockModelData = [
store.createRecord('real-model', {
category: 'Leopard',
childCategories: [],
parentCategory: null
}),
store.createRecord('real-model', {
category: 'Snow Leopard',
childCategories: [],
parentCategory: 'Leopard'
}),
store.createRecord('real-model', {
category: 'Persian Leopard',
childCategories: [],
parentCategory: 'Leopard'
})
]
let processedData = route._postModelProcessing(mockModelData);
let leopardFirstChild = processedData[0].get('childCategories')[0];
assert.equal(leopardFirstChild.get('category'), 'Snow Leopard');
})
});
当我尝试通过在我的路由(单元)测试中写入 let mockModel = this.owner.lookup('model:realModel');
来模拟 model
时,我收到错误
Error: You should not call 'create' on a model. Instead, call 'store.createRecord' with the attributes you would like to set.
模拟 model
以测试 setupController
和其他私有路由方法中的处理的正确方法是什么?
说明测试setupController/private路由方法的代码:
app\routes\application.js
export default Route.extend({
_postModelProcessing(inputModel){
....
return processedModelData;
},
setupController(controller, model){
let processedModelData = _postModelProcessing(model);
}
})
tests\unit\routes\application-test.js
module('Unit | Route | application', function(hooks) {
setupTest(hooks);
...
test('private _postModelProcessing correctly processes model data', function(assert) {
let route = this.owner.lookup('route:application');
// This line throws the Error mentioned above
let mockModel = this.owner.lookup('model:realModel');
// what I hoped to do, if the above line didn't throw an error:
let mockModelData = [
mockModel.create({
category: 'Leopard',
childCategories: [],
parentCategory: null
}),
mockModel.create({
category: 'Snow Leopard',
childCategories: [],
parentCategory: 'Leopard'
}),
mockModel.create({
category: 'Persian Leopard',
childCategories: [],
parentCategory: 'Leopard'
})
]
let processedData = route._postModelProcessing(mockModelData);
let leopardFirstChild = processedData[0].get('childCategories')[0];
assert.equal(leopardFirstChild.get('category'), 'Snow Leopard');
})
});
正如错误信息所说,使用store.createRecord
:
module('Unit | Route | application', function(hooks) {
setupTest(hooks);
...
test('private _postModelProcessing correctly processes model data', function(assert) {
let route = this.owner.lookup('route:application');
let store = this.owner.lookup('service:store');
// what I hoped to do, if the above line didn't throw an error:
let mockModelData = [
store.createRecord('real-model', {
category: 'Leopard',
childCategories: [],
parentCategory: null
}),
store.createRecord('real-model', {
category: 'Snow Leopard',
childCategories: [],
parentCategory: 'Leopard'
}),
store.createRecord('real-model', {
category: 'Persian Leopard',
childCategories: [],
parentCategory: 'Leopard'
})
]
let processedData = route._postModelProcessing(mockModelData);
let leopardFirstChild = processedData[0].get('childCategories')[0];
assert.equal(leopardFirstChild.get('category'), 'Snow Leopard');
})
});