Hi,
I have the following test for a component:
test('it renders', withChai(function(expect) {
this.render(hbs`{{navigation-bar}}`);
expect(this.$().text().trim()).to.equal('');
this.render(hbs`
{{#navigation-bar}}
template block text
{{/navigation-bar}}
`);
expect(this.$().text().trim()).to.equal('template block text');
}));
This component injects the session service, meaning I would like to be able to test the served content both when signed in and when signed out.
Unfortunately, it doesn't seem like I have access to the application object within this context, and I therefore cannot use authenticateSession.
What would be the recommended way to authenticating or deauthenticating in integration tests like this one?
The test helpers that ESA provides are only supposed to be used in acceptance tests. In an integration test, the best way to stub a session in a particular state is to stub the session service.
Thank you Marco. This solution works perfectly for me.
Sorry if this is stupid/old, but would you mind giving some detail on how to stub this?
This approach:
const sessionStub = Ember.Service.extend({
isAuthenticated: true,
authenticate() {
this.isAuthenticated = true;
},
invalidate() {
this.isAuthenticated = false;
}
});
moduleForComponent('login-button', 'Integration | Component | login button', {
integration: true,
beforeEach() {
this.register('service:session-service', sessionStub);
this.inject.service('session-service', {as: 'session'});
}
});
Simply doesn't seem to work. I've seen the question come up a few times, too, perhaps this could be documented somewhere?
@TLATER: that's generally the correct approach.
I've got the same problem in Ember 3.1 and the new tests syntax - stubbing session service just doesn't work. And it works when I change service name (in component and in tests). So it seems that Ember Simple Auth somehow overrides my stubbed service..? Integration testing example in docs would be awesome!
Ok, it seems it's rather ember-cli-qunit problem with stubbing services that are looked up in initializers.
I use this workaround:
hooks.beforeEach(function() {
this.owner.lookup('service:session').setProperties({
user: { id: 2 }
});
});
@jembezmamy Worked flawlessly! Thanks a billion :)
Most helpful comment
Ok, it seems it's rather ember-cli-qunit problem with stubbing services that are looked up in initializers.
I use this workaround: