vendredi 12 octobre 2018

How to mock and test Model operations in Ember route

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.

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

  })

});




Aucun commentaire:

Enregistrer un commentaire