Skip to content
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

moduleForModel doesn't play nicely with application serializer. #165

Closed
nathanhammond opened this issue Jul 2, 2016 · 6 comments
Closed

Comments

@nathanhammond
Copy link
Member

Original report here: ember-cli/ember-cli#4879

/cc @dustinfarris @stefanpenner

@oligriffiths
Copy link

Hey @nathanhammond what's the state of play with this. I'm getting that error when implementing an application serlializer test in 2.7.0 😞

@nathanhammond
Copy link
Member Author

@oligriffiths It's still an open issue, we've tracked it down to this file, but nobody has investigated how to make it work.

Would you be willing to take a swing at it?

@jangxyz
Copy link

jangxyz commented Oct 28, 2016

I just bumped into this. I'm now sure if this is the right way, but let me introduce the way I figured.

Why fail

  1. I generated an ApplicationSerializer to make a custom action (typical keyForAttribute override). ember-cli created a test for me at: tests/unit/serializers/application-test.js.

    At this time I already had another model minute, but I just wanted to make some default behavior. I have my own serializer for the that as well, along with the test.

  2. Serializer tests start with moduleForModel.

  3. As "application" is passed in to moduleForModel, TestModuleForModel tries to find a model with such name, but fails. Hence error.

    TypeError: Attempting to register an unknown factory: `model:application`
    

How I solved it

For my use case, since I already have a separate model, all I need to do was pass the name of that model. I just patched TestModuleForModel class to read an optional modelName from the callbacks parameter -- it seemed like where you pass options.

import { TestModule } from 'ember-test-helpers';

const TestModuleForModelWithName = TestModule.extend({
  init: function(modelName, description, callbacks) {
    this.modelName = callbacks.modelName || modelName; // CHECK FOR callbacks.modelName FIRST

    this._super.call(this, 'model:' + this.modelName, description, callbacks);

    this.setupSteps.push(this.setupModel);
  },
  // ...
});

Since I couldn't use the default moduleForModel function, I just directly called createModule in my test.

import { createModule } from 'ember-qunit/qunit-module';

createModule(TestModuleForModelWithName, 'application', 'Unit | Serializer | application', {
  // Specify the other units that are required for this test.
  needs: ['serializer:application'],
  modelName: 'minute',
});

In general

However I can't think of a way to let ember-cli generate a sane test that passes by default, because it can't figure out which model to use when you create an application serializer.
It would be nice for the default TestModuleForModel class to accept modelName option like the patch though, so people could easily get it figured.

Maybe generating the modelName option with a comment will help others to figure it out?

What do you think?

@nathanhammond
Copy link
Member Author

@jangxyz This is a unique situation and we can likely write one-off code to support it. Is that something that you would be willing to undertake and write a PR for?

@skarger
Copy link

skarger commented Jul 22, 2017

I just had this problem with circumstances similar to what @jangxyz described above.

$ ember version
ember-cli: 2.13.2

I solved it by running ember generate model application.
Then the auto-generated tests/unit/serializers/application-test.js passes as-is, specifically this line:
moduleForModel('application', 'Unit | Serializer | application', {

It seems vaguely wrong to generate an extraneous model but it was a simple fix.

armellarcier added a commit to armellarcier/ember-ghibliapi-browser that referenced this issue Sep 2, 2017
…erated tests do pass

When you generate the application serializer, the tests will fail with error :
`Attempting to register an unknown factory: 'model:application'`

This has been reported on github : ember-cli/ember-cli#4879
I found the fix in a comment : emberjs/ember-test-helpers#165 (comment)

This `hack` feels wrong but seems to be the simplest.
@rwjblue
Copy link
Member

rwjblue commented Nov 30, 2017

This should be addressed when using the new testing APIs provided by ember-qunit (e.g. moduleForModel is no longer "a thing").

Closing....

@rwjblue rwjblue closed this as completed Nov 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants