简体   繁体   中英

How to mock model when testing setupController or private Route methods

When I try to mock model by writing let mockModel = this.owner.lookup('model:realModel'); in my route (unit) test, I receive the error Error: You should not call 'create' on a model. Instead, call 'store.createRecord' with the attributes you would like to set. Error: You should not call 'create' on a model. Instead, call 'store.createRecord' with the attributes you would like to set.

What's the proper way to mock model to test processing within setupController and other private route methods?

Code to illustrate testing setupController/private route methods:

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');

  })

});

Just what the error message says, use 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');

  })

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM