-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to unit test a model using PartialModel extension? #30
Comments
Hi @bsaff The problem is most likely related to the fact that partial extensions are lazily generated only when using |
Hey @Azdaroth, thank you very much! That was exactly what I needed. here's my workaround: // app_name/tests/helpers/partial-model-helper.js
import Ember from 'ember';
import DS from 'ember-data';
const { Model } = DS;
export function generatePartialExtensionModel(factoryName, factory) {
factory.eachRelationship((relationshipKey, descriptor) => {
let partialExtensionModelName = `${factoryName}-${relationshipKey}`;
let dependencyName = `model:${partialExtensionModelName}`;
if (descriptor.options.isPartialExtension === true) {
if (!_isDependencyRegistered(this, dependencyName)) {
let partialExtensionModel = Model.extend(descriptor.options.classHash)
.reopenClass({ _extendPartialModel: factoryName });
_registerDependency(this, dependencyName, partialExtensionModel);
}
}
});
}
function _registerDependency(store, dependencyName, dependency) {
if (Ember.getOwner) {
Ember.getOwner(store).register(dependencyName, dependency);
}
}
function _isDependencyRegistered(store, dependencyName) {
if (Ember.getOwner) {
return Ember.getOwner(store).hasRegistration(dependencyName);
}
} // app_name/tests/unit/models/car.js
import { moduleForModel, test } from 'ember-qunit';
import { generatePartialExtensionModel } from 'market-ember/tests/helpers/partial-model-helper';
moduleForModel('car', 'Unit | Model | car', {
needs: [],
beforeEach() {
const car = this.store().modelFor('car');
generatePartialExtensionModel.call(this, 'car', car);
},
});
test('it exists', function(assert) {
var model = this.subject();
assert.ok(!!model);
}); |
Hmm, this is certainly too complex, it should really be way simpler than that with some extra helper, but glad to hear you've managed to handle it. |
After extending my model, call it
car
, from PartialModel I can't run ember unit test. Tests fail with the following error:Tried including in unit test dependency like so:
but that doesn't work either.
Any suggestions on how to get unit tests working?
Model def:
versions:
"ember-data": "2.13",
"ember-data-partial-model": "0.4.0",
"ember-source": "~2.15.0",
The text was updated successfully, but these errors were encountered: