Когда я пытаюсь смоделировать model
, написав let mockModel = this.owner.lookup('model:realModel');
в моем тесте маршрута (юнит), я получаю ошибку
Error: You should not call 'create' on a model. Instead, call 'store.createRecord' with the attributes you would like to set.
Как правильно смоделировать model
для проверки обработки вsetupController
и другие методы частного маршрута?
Код для иллюстрации тестирования setupController / методы частного маршрута:
app \ rout \ application.js
export default Route.extend({
_postModelProcessing(inputModel){
....
return processedModelData;
},
setupController(controller, model){
let processedModelData = _postModelProcessing(model);
}
})
tests \ unit \ route \ 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');
})
});