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

resizeService not injected in test environment #43

Closed
timiyay opened this issue May 19, 2016 · 7 comments
Closed

resizeService not injected in test environment #43

timiyay opened this issue May 19, 2016 · 7 comments

Comments

@timiyay
Copy link

timiyay commented May 19, 2016

Versions
ember 2.4.4
ember-resize 0.0.12

Problem
I recently integrated ember-resize into a component, which broke my integration tests. The component rendered and behaved as expected during development, but test runs would fail.

Even though I stubbed the resizeService as per the Ember component testing docs, the tests failed due to resizeService being undefined.

It appears that ember-resize does not inject the service in the test environment.

Example Test:

// tests/integration/components/my-component-test.js

const resizeServiceStub = Ember.Service.extend({
  on() {}
});

test('it renders', function(assert) {
    this.register('service:resize', resizeServiceStub);
    this.inject.service('resize', { as: 'resizeService' });

    this.render(hbs`{{my-component}}`);
});

// TEST FAILED: TypeError: Cannot read property 'on' of undefined

Workaround

The problem can be fixed by manually-injecting the service into your component, in your app code

// app/components/my-component.js

resizeService: Ember.inject.service('resize')

This injection is redundant outside of the test environment, but required within it.

@mike-north
Copy link
Owner

I think the solution here is to clarify in the README what needs to be done. I'm wary about injecting services into unit tests without the developer being explicit about it

@timiyay
Copy link
Author

timiyay commented May 23, 2016

In this case, we're talking about integration tests, rather than unit tests.

For mine, the expected behaviour for component integration tests is that service injection mirrors development/prod. If a service is injected in development/prod, it's injected in test. This matches the behaviour of other service-injecting Ember add-ons.

In my case, I had to change my production code to enable test-specific behaviour. I'm already being explicit about the fact I need the resizeService, as per the Ember testing docs.

The bug is that, even after I register the service with

this.register('service:resize', resizeServiceStub);
this.inject.service('resize', { as: 'resizeService' });

...it's still not available in my tests.

@Fryie
Copy link

Fryie commented Jul 20, 2016

I could be wrong, but afaik you should always inject services even in your production code. That is, resizeService: Ember.inject.service('resize') is exactly the way you should do it.
I'd even go so far as to suggest that this addon should not automatically inject itself via an initializer.

@localpcguy
Copy link

My expectation (and I could have the wrong expectation) is that if I don't need to inject the service in my component in order for it to work, and integration test should not then require me to inject it. You end up in a state where your code works, but the tests fail. While updating the documentation could make this easier to debug when a dev comes across it, personally, I think it would be better to either

  1. work without the service being injected by the developer just to make the test work
  2. require the service to be injected by the developer in order to use the mixin (thereby also working in the test)

@myartsev
Copy link

myartsev commented May 24, 2017

It's been a while since the last update, just wanted to throw out a +1 that this is an issue. Especially in terms of time it took me fiddling around with tests and finally finding this issue on github.

For now I'm going to (redundantly) inject the service into production code.
My intuition is that this should be the default behaviour, ember-resize should not automatically inject itself into every component. I'm fairly new to Ember, but there is something off here; changing production code to enable test specific behaviour.

@chriskrycho
Copy link

I've started seeing this with acceptance tests after upgrading to Ember 2.13.

mike-north pushed a commit that referenced this issue Aug 23, 2018
## [0.2.1](v0.2.0...v0.2.1) (2018-08-23)

### Bug Fixes

* ensure resize service is present in tests ([b7a518b](b7a518b))
* ensure resize service is present in tests fixes [#43](#43) ([d2fa677](d2fa677))
* remove ember-decorators ([eb3275e](eb3275e))
@mike-north
Copy link
Owner

🎉 This issue has been resolved in version 0.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants