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

Injecting services for component integration tests doesn't seem to work #114

Closed
devinus opened this issue Oct 28, 2015 · 13 comments
Closed

Comments

@devinus
Copy link
Member

devinus commented Oct 28, 2015

The following code using this.inject.service never seems to inject the service into the component before trying to render.

/* jshint expr:true */
import { expect } from 'chai';
import {
  describeComponent,
  it
} from 'ember-mocha';
import hbs from 'htmlbars-inline-precompile';

describeComponent(
  'provider-options',
  'Integration: ProviderOptionsComponent',
  {
    integration: true
  },
  function() {
    it('renders', function() {
      // Set any properties with this.set('myProperty', 'value');
      // Handle any actions with this.on('myAction', function(val) { ... });
      // Template block usage:
      // this.render(hbs`
      //   {{#provider-options}}
      //     template content
      //   {{/provider-options}}
      // `);

      this.inject.service('i18n'); // <= This doesn't seem to inject the i18n service
      this.render(hbs`{{provider-options}}`);
      expect(this.$()).to.have.length(1);
    });
  }
);
@rwjblue
Copy link
Member

rwjblue commented Oct 28, 2015

Use the service name only (not the type and the name):

this.inject.service('i18n')

@devinus
Copy link
Member Author

devinus commented Oct 30, 2015

I'm sorry, I actually was using this.inject.service('i18n') and filed the issue with the code after a small edit. But this.inject.service('i18n') doesn't inject it either.

@rwjblue
Copy link
Member

rwjblue commented Oct 31, 2015

Hmm. It should be working. Can you make a demo repo that I can use to track down the issue your are seeing?

@devinus
Copy link
Member Author

devinus commented Nov 2, 2015

@rwjblue To clarify, this.inject.service('i18n') should inject i18n into the component itself, so from the component this.get('i18n) would be the service, correct?

@rwjblue
Copy link
Member

rwjblue commented Nov 2, 2015

this.inject.service is only available in tests, and injects the instance of service:i18n into the test context (not the component(s) under test).

Your component would still need i18n: Ember.inject.service() in it also.

@devinus
Copy link
Member Author

devinus commented Nov 2, 2015

That was my problem then--a misunderstanding. I have an application injection inject i18n into all components, is there no way to inject it at test time?

@rwjblue
Copy link
Member

rwjblue commented Nov 2, 2015

initializers do not run in integration tests, I suppose in general you could import the initializer function and call it from your setup/beforeEach block, but that seems much less useful.

I find using foo: Ember.inject.service() in my components to be much better than attempting to globally inject things into all components for a couple reasons:

  • The previously mentioned issue with integration tests.
  • A large percentage of the time, you do not actually need all services in all components.
  • Declaring foo: Ember.inject.service() inside your component definition makes it super clear where this magical foo property is coming from.

@rwjblue
Copy link
Member

rwjblue commented Nov 2, 2015

I'm going to close this, as I believe we are now on the same page

@rwjblue rwjblue closed this as completed Nov 2, 2015
@devinus
Copy link
Member Author

devinus commented Nov 2, 2015

Yep, not a bug. Thanks @rwjblue

@devinus
Copy link
Member Author

devinus commented Nov 2, 2015

For posterity, another alternative is passing in the service when you call render. E.g.

this.render(hbs{{provider-options i18n=i18n}});

@rwjblue
Copy link
Member

rwjblue commented Nov 2, 2015

Yep

@rwjblue
Copy link
Member

rwjblue commented Nov 2, 2015

Though I personally prefer to test the actual template invocation that will be used.

@devinus
Copy link
Member Author

devinus commented Nov 2, 2015

Yeah, I think I prefer just injecting it manually. I'm all for self-documenting code, and doing this in 1 line versus 2 in every test is a no brainer.

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

2 participants