Search code examples
unit-testingember.jsdependency-injectionqunitember-simple-auth

Unknown `service:session` injector in Ember route unit test


I have simple sign in route in my Ember app:

import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin, {
});

And not much more complicated unit test for that:

import { moduleFor, test } from 'ember-qunit';

moduleFor('route:sign-in', 'Unit | Route | sign in', {
  needs: ['service:session']
});

test('it exists', function(assert) {
  let route = this.subject();
  assert.ok(route);
});

But when running ember test -f sign it fails with error telling me that it cannot inject service:session:

Built project successfully. Stored in "/Users/hauleth/Workspace/hauleth/crossover/frontend/tmp/core_object-tests_dist-P16KQenq.tmp".
not ok 1 PhantomJS 2.1 - Unit | Controller | sign in: it exists
    ---
        actual: >
            null
        stack: >
            validateInjections@http://localhost:7357/assets/vendor.js:12932:91
            http://localhost:7357/assets/vendor.js:12107:48
            runInDebug@http://localhost:7357/assets/vendor.js:17219:9
            runInDebug@http://localhost:7357/assets/vendor.js:26382:43
            instantiate@http://localhost:7357/assets/vendor.js:12101:34
            lookup@http://localhost:7357/assets/vendor.js:11955:28
            buildInjections@http://localhost:7357/assets/vendor.js:11994:42
            injectionsFor@http://localhost:7357/assets/vendor.js:12067:37
            factoryFor@http://localhost:7357/assets/vendor.js:12038:37
            lookupFactory@http://localhost:7357/assets/vendor.js:11888:24
            factory@http://localhost:7357/assets/test-support.js:7816:39
            http://localhost:7357/assets/test-support.js:7904:75
            http://localhost:7357/assets/tests.js:345:34
            wrapper@http://localhost:7357/assets/test-support.js:6634:34
            runTest@http://localhost:7357/assets/test-support.js:2779:32
            run@http://localhost:7357/assets/test-support.js:2764:11
            http://localhost:7357/assets/test-support.js:2906:14
            process@http://localhost:7357/assets/test-support.js:2565:24
            begin@http://localhost:7357/assets/test-support.js:2547:9
            http://localhost:7357/assets/test-support.js:2607:9
        message: >
            Died on test #1 testWrapper@http://localhost:7357/assets/test-support.js:6663:16
            test@http://localhost:7357/assets/test-support.js:6676:44
            http://localhost:7357/assets/tests.js:344:24
            exports@http://localhost:7357/assets/vendor.js:131:37
            requireModule@http://localhost:7357/assets/vendor.js:30:25
            require@http://localhost:7357/assets/test-loader.js:67:16
            loadModules@http://localhost:7357/assets/test-loader.js:58:25
            load@http://localhost:7357/assets/test-loader.js:89:35
            http://localhost:7357/assets/test-support.js:6485:20: Attempting to inject an unknown injection: `service:session`
        Log: |
    ...
ok 2 PhantomJS 2.1 - Unit | Route | sign in: it exists

1..2
# tests 2
# pass  1
# skip  0
# fail  1

I tried to find solutions but everything I find is about integration testing or tells me to add needs: ['service:session'], but it already is there.


Solution

  • Happens to me too. I think it's because the session service can't be found within your app (it's in the addon). Don't take my word for it though.

    Anyways, what you can do is stub the session object itself in the route subject rather than in needs.

    e.g.

    test('it exists', function(assert) {
      let route = this.subject({session: Ember.Object.create()});
      assert.ok(route);
    });
    

    The problem there is if you need something in the session like session.get('isAuthenticated') it will fail because you simply stubbed an empty session. So you should instead stub it like this:

    let route = this.subject({session: Ember.Object.create({isAuthenticated: true});
    

    Tip: Creating a test-helper to create that session stub would be useful.