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

Allow overriding services/factories in the registry. #96

Merged
merged 1 commit into from
Aug 19, 2015

Conversation

rwjblue
Copy link
Member

@rwjblue rwjblue commented Aug 19, 2015

By default in Ember things that are automatically resolved take precedence over things that are manually registered. This is somewhat counterintuitive to many people, as they would like to be able to override a service or other factory in a given set of tests (for example to use a mock analytics service).

The ability to do this has existed since the SSR/Fastboot work that split the registry from the container in 1.11, but I only recently realized how I could leverage that work here.

This change adds new functionality on Ember 1.11+, but allows older versions to continue to work as they do today (aka non-breaking).


Example:

Assuming you have a app/services/analytics.js and want to use a mock service for a set of component integration tests, you might do something like this:

import MockAnalyticsService from '../mock-services/analytics';
import { moduleForComponent, test } from 'ember-qunit';

moduleForComponent('something-cool', {
  integration: true,
  beforeEach() {
    this.registry.register('service:analytics', MockAnalyticsService);
  }
});

In the above scenario, the mock service would be used instead of the one at app/services/analytics.js.

By default in Ember things that are automatically resolved take
precedence over things that are manually registered. This is somewhat
counterintuitive to many people, as they would like to be able to
override a service or other factory in a given set of tests (for example
to use a mock analytics service).

The ability to do this has existed since the SSR/Fastboot work that
split the registry from the container in 1.11, but I only recently
realized how I could leverage that work here.

This change adds new functionality on Ember 1.11+, but allows older
versions to continue to work as they do today (aka non-breaking).

---

Example:

Assuming you have a `app/services/analytics.js` and want to use a mock
service for a set of component integration tests, you might do something
like this:

```javascript
import MockAnalyticsService from '../mock-services/analytics';
import { moduleForComponent, test } from 'ember-qunit';

moduleForComponent('something-cool', {
  integration: true,
  beforeEach() {
    this.registry.register('service:analytics', MockAnalyticsService);
  }
});
```

In the above scenario, the mock service would be used instead of the
one at `app/services/analytics.js`.
@rwjblue
Copy link
Member Author

rwjblue commented Aug 19, 2015

I stole this from recent work @dgeb did with container / registry reform in Ember itself, and confirmed that the fallback registry works properly in Ember 1.11+.

@dgeb - Would you mind reviewing?

dgeb added a commit that referenced this pull request Aug 19, 2015
Allow overriding services/factories in the registry.
@dgeb dgeb merged commit fcb38b2 into emberjs:master Aug 19, 2015
@dgeb
Copy link
Member

dgeb commented Aug 19, 2015

Great work @rwjblue!

@rwjblue rwjblue deleted the allow-registry-overrides branch August 20, 2015 00:56
@rondale-sc
Copy link

Does this only apply to component integration tests? Or can this be more widely applied to acceptance tests?

@rondale-sc
Copy link

I was unable to reference this.registry from an acceptance test is why I ask. btw.

@rwjblue
Copy link
Member Author

rwjblue commented Aug 22, 2015

Acceptance tests generally do not use ember-qunit, so this doesn't do anything for them.

More work to come for acceptance tests...

@rondale-sc
Copy link

Right on! Side note, this is awesome!

@tim-evans
Copy link
Contributor

Could this be extended to stub out modules as well? (this.requirejs.entries)

@rwjblue
Copy link
Member Author

rwjblue commented Aug 23, 2015

@tim-evans - Generally, I would rather not do that. What is your use-case?

@tim-evans
Copy link
Contributor

Mocking out functions that are the only export from a file.

This is for the testing story of the "actions" RFC that I proposed.

@rwjblue
Copy link
Member Author

rwjblue commented Aug 23, 2015

If the functions are looked up via the container/resolver, it will already work...

@tim-evans
Copy link
Contributor

Hmm... I'll test that! Seems like a nice way to do that. (They're currently using import)

@mixonic
Copy link
Member

mixonic commented Sep 9, 2015

This requires documentation.

@rwjblue
Copy link
Member Author

rwjblue commented Sep 9, 2015

@mixonic - TBH, I'm not sure we can document it. AFAIK, registry is private.

@rwjblue
Copy link
Member Author

rwjblue commented Sep 9, 2015

Also, this project contains no documentation (though we could document in the ember-qunit README which is a bit more fleshed out).

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

Successfully merging this pull request may close these issues.

5 participants