chancancode / ember-polaris-service

Previewing Ember Polaris style services
Other
13 stars 3 forks source link

Accessing service instance in testing (to `sinon.stub()` on it) #20

Closed gossi closed 5 months ago

gossi commented 5 months ago

In a classic ember service I have the following test:

  test('playTrack', function (assert) {
    const spotifyService = this.owner.lookup('service:spotify');
    const spotifyClient = spotifyService.client;
    const play = sinon.stub(spotifyClient.api, 'play'); // <-- here !

    playTrack(this.owner)(RADIOACTIVE);
    assert.true(
      play.calledOnceWith({
        uris: [RADIOACTIVE.uri]
      })
    );
  });

now, with this addon, I can "only override?" the entire service class? What's the intended way to write tests like above?

gossi commented 5 months ago

Oh welp, it was too trivial:

import { getContext } from '@ember/test-helpers';
import type { TestContext } from '@ember/test-helpers';

// ...

const spotifyService = service(getContext() as TestContext, SpotifyService);

Closing, but for people coming here via google. That's the solution =)