ember-qunit-codemod icon indicating copy to clipboard operation
ember-qunit-codemod copied to clipboard

How to convert unit tests with injections?

Open alexander-alvarez opened this issue 8 years ago • 5 comments

I have some injections on my controller (inside of an engine) which worked, but after running the codemod I'm getting errors:

Attempting to inject an unknown injection: 'router:main

I imagine it's because the registry in the new API is isolated?

Should we be manually registering services, or is there a different desired change?

const resolver = engineResolverFor('my-engine');

module('Unit | Controller | my controller', function(hooks) {
  setupTest(hooks, {
    resolver
  });

  test('injection of services', function(assert) {
      // error occurs here
      let controller = this.owner.factoryFor('controller:my-controller').create({});
    });

});

alexander-alvarez avatar Nov 14 '17 00:11 alexander-alvarez

I need to see how this actually worked before, I'm not sure the best path forward here.

I believe that this might have been fixed automatically when using [email protected] and use setApplication (like https://github.com/ember-cli/ember-cli/pull/7437). Can you double check if using setApplication properly adds router:main to the registry?

rwjblue avatar Nov 14 '17 01:11 rwjblue

On second thought, I'm not 100% sure if we also need changes to the engineResolverFor helper method also (and internally create the engine instance exactly how ember itself creates them normally)...

rwjblue avatar Nov 14 '17 01:11 rwjblue

@alexander-alvarez - Any chance you could whip up a demo repo that I could use to step through?

rwjblue avatar Nov 17 '17 18:11 rwjblue

No problem... I'll try to set something up Monday night

alexander-alvarez avatar Nov 17 '17 20:11 alexander-alvarez

been working on https://github.com/alexander-alvarez/ember-qunit-engine-injection-repro/commits/master

So I don't know if it's just not my day, but I'm getting a lot of weird behavior on a clean branch. I can't seem to test with a live service injection into a controller in the old mode, as well as the new mode. https://github.com/alexander-alvarez/ember-qunit-engine-injection-repro/blob/master/tests/unit/controllers/application-test.js#L10

Except for registering models (how should we do this ?) it seems the new style is working https://github.com/alexander-alvarez/ember-qunit-engine-injection-repro/blob/master/tests/unit/controllers/controller-with-model-transformed-test.js

Sorry this isn't much help. Been an awfully head-scratchy night

alexander-alvarez avatar Nov 21 '17 04:11 alexander-alvarez