Unit testing Mixin model

I am trying to run a basic “this works” test for Mixin, which is intended to be used with a model. I suggest that the Mixin unit testing approach should not be done by Mixin itself, but by the generic Model class, in which Mixin is mixed.

Assuming this first assumption / strategy makes sense, here is what I tried to do:

import DS from 'ember-data'; import Ember from 'ember'; import DictionaryManagerMixin from 'trainer/mixins/dictionary-manager'; module('DictionaryManagerMixin'); test('it works', function() { var DictionaryManagerModel = DS.Model.extend(DictionaryManagerMixin, { title: DS.attr('string') }); var myStore = DS.Store.create(); var subject = myStore.createRecord(DictionaryManagerModel); ok(subject); }); 

This does not work, resulting in the following error:

 TypeError: Cannot read property 'lookup' of undefined at null.<anonymous> (http://localhost:4200/assets/vendor.js:95610:35) at Descriptor.ComputedPropertyPrototype.get (http://localhost:4200/assets/vendor.js:28466:25) at get (http://localhost:4200/assets/vendor.js:33944:21) at Ember.Object.extend.adapterFor (http://localhost:4200/assets/vendor.js:97005:27) at Ember.Object.extend._generateId (http://localhost:4200/assets/vendor.js:95682:28) at Ember.Object.extend.createRecord (http://localhost:4200/assets/vendor.js:95654:32) at Object.eval (trainer/tests/unit/mixins/dictionary-manager-test.js:17:28) at Object.Test.run (http://localhost:4200/assets/test-support.js:2632:18) at http://localhost:4200/assets/test-support.js:2719:10 at process (http://localhost:4200/assets/test-support.js:2435:24) 

Any help would be greatly appreciated.

+6
source share
1 answer

Usually a lookup requires a container, so a hint of what I think is causing the problem. This makes sense because DS relies on the container to be able to view registered models in model:model-name ;

So your test dependency for this mixin is really on the correct setting for Ember Data. Therefore, if you conduct a test to make it work for the Ember Data model, mixin more or less just snaps into place with the object set up as it expects.

I would try using moduleForModel from the useful Ember doc :

 moduleForModel('dictionary-manager-model'); test('your test here', function(assert) { // this.subject aliases the createRecord method on the model const dictionaryManagerModel = this.subject(); }); 

Do you need to conditionally translate to mixin with a model? If your model always uses mixin, you can do this in the model definition file and simply test it as shown above. In your example, mixin is added to the model and the model is passed to createRecord`, but this is not recommended:

https://github.com/emberjs/data/blob/v2.14.10/addon/-private/system/store.js#L351 assert( Removing classes for storing methods has been deleted. Please pass dasherized string instead of $ {modelName} , typeof modelName === 'string'); Therefore, we rely on container searches.

Essentially, I think you passed the model test, not the mixin test. If the mixture cannot be flipped into something that is not a model and still works.

ember generate model-test dictionary-manager-model if it does not already exist and then your model file mixes the mixture. Besides, maybe this should not be a separate mixin?

Hope this helps you get started in the right direction, cheers! ✌🏽

0
source

Source: https://habr.com/ru/post/979563/


All Articles