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

Add public API for injection & registration #105

Merged
merged 1 commit into from Oct 1, 2015

Conversation

Projects
None yet
3 participants
@ef4
Contributor

ef4 commented Sep 30, 2015

In an app, we encourages users to use injection over container.lookup,
and we encourage application.register over registry.register. But in our integration tests, there is no good alternative to direct this.container.lookup and this.registry.register.

So I added register and inject capability directly to the integration test context. register is fairly trivial:

    test('registering something', function(assert) {
      this.register('component:x-foo', Ember.Component.extend({
        classNames: ['i-am-x-foo']
      }));
      this.render(hbs`{{x-foo}}`);
      assert.equal(this.$('.i-am-x-foo').length, 1, 'found x-foo');
    });

inject lets you inject anything into the test context:

    test('injecting a service', function(assert) {
      this.inject.service('notifications');
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context,
      // so we can access it here and tell it to do something.
      this.get('notifications').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })

You can also give the injected thing an alternate name using as:

    test('injecting a service with aliased name', function(assert) {
      this.inject.service('notifications', { as: 'messages' });
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context
      // as this.get('messages').
      this.get('messages').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })
@pixelhandler

This comment has been minimized.

Show comment
Hide comment
@pixelhandler

pixelhandler Sep 30, 2015

This looks nice! I've been reaching into the container and using register for a while in tests but it will be great to have a blessed path to doing so 👍

pixelhandler commented Sep 30, 2015

This looks nice! I've been reaching into the container and using register for a while in tests but it will be great to have a blessed path to doing so 👍

Add public API for injection & registration
In an app, we encourages users to use injection over container.lookup,
and we encourage `application.register` over `registry.register`. But in
our integration tests, there is no good alternative to direct
`this.container.lookup` and `this.registry.register`.

So I added `register` and `inject` capability directly to the
integration test context. `register` is fairly trivial:

    test('registering something', function(assert) {
      this.register('component:x-foo', Ember.Component.extend({
        classNames: ['i-am-x-foo']
      }));
      this.render(hbs`{{x-foo}}`);
      assert.equal(this.$('.i-am-x-foo').length, 1, 'found x-foo');
    });

`inject` lets you inject anything into the test context:

    test('injecting a service', function(assert) {
      this.inject.service('notifications');
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context,
      // so we can access it here and tell it to do something.
      this.get('notifications').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })

You can also give the injected thing an alternate name using `as`:

    test('injecting a service with aliased name', function(assert) {
      this.inject.service('notifications', { as: 'messages' });
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context
      // as this.get('messages').
      this.get('messages').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })
@ef4

This comment has been minimized.

Show comment
Hide comment
@ef4

ef4 Oct 1, 2015

Contributor

I moved the methods into the base test-context so they're available in all test types, and updated some tests to show that they work.

Contributor

ef4 commented Oct 1, 2015

I moved the methods into the base test-context so they're available in all test types, and updated some tests to show that they work.

rwjblue added a commit that referenced this pull request Oct 1, 2015

Merge pull request #105 from ef4/injection-api
Add public API for injection & registration

@rwjblue rwjblue merged commit 5c887f5 into emberjs:master Oct 1, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment