1

내 엠버 응용 프로그램에서 경로에서 간단한 기호를 가지고 :알 수없는`서비스 : 엠버 경로 단위 테스트에서 session` 인젝터

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

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

그리고 그것에 대해별로 더 복잡한 단위 테스트 :

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); 
}); 

ember test -f sign를 실행하지만 그것은 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: > 
      [email protected]://localhost:7357/assets/vendor.js:12932:91 
      http://localhost:7357/assets/vendor.js:12107:48 
      [email protected]://localhost:7357/assets/vendor.js:17219:9 
      [email protected]://localhost:7357/assets/vendor.js:26382:43 
      [email protected]://localhost:7357/assets/vendor.js:12101:34 
      [email protected]://localhost:7357/assets/vendor.js:11955:28 
      [email protected]://localhost:7357/assets/vendor.js:11994:42 
      [email protected]://localhost:7357/assets/vendor.js:12067:37 
      [email protected]://localhost:7357/assets/vendor.js:12038:37 
      [email protected]://localhost:7357/assets/vendor.js:11888:24 
      [email protected]://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 
      [email protected]://localhost:7357/assets/test-support.js:6634:34 
      [email protected]://localhost:7357/assets/test-support.js:2779:32 
      [email protected]://localhost:7357/assets/test-support.js:2764:11 
      http://localhost:7357/assets/test-support.js:2906:14 
      [email protected]://localhost:7357/assets/test-support.js:2565:24 
      [email protected]://localhost:7357/assets/test-support.js:2547:9 
      http://localhost:7357/assets/test-support.js:2607:9 
     message: > 
      Died on test #1 [email protected]://localhost:7357/assets/test-support.js:6663:16 
      [email protected]://localhost:7357/assets/test-support.js:6676:44 
      http://localhost:7357/assets/tests.js:344:24 
      [email protected]://localhost:7357/assets/vendor.js:131:37 
      [email protected]://localhost:7357/assets/vendor.js:30:25 
      [email protected]://localhost:7357/assets/test-loader.js:67:16 
      [email protected]://localhost:7357/assets/test-loader.js:58:25 
      [email protected]://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 

솔루션을 찾으려고했지만 통합 테스트에 관한 것이거나 needs: ['service:session']을 추가하라고 알려졌지만 이미 있습니다.

답변

1

나에게도 일어납니다. 나는 session 서비스가 귀하의 응용 프로그램 (addon에 있음)에서 찾을 수 없기 때문이라고 생각합니다. 그래도 내 말을 듣지 마라.

어쨌든 당신이 할 수있는 일은 세션 객체 자체를 필요보다는 경로 대상에 스텁 (stub)하는 것입니다.

예컨대 당신은 단순히 빈 session를 스텁 때문에 실패합니다 session.get('isAuthenticated') 같은 세션에서 뭔가를 필요로하는 경우

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

문제가있다. 따라서 다음과 같이 스텁해야합니다.

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

팁 : 세션 스텁을 생성하기위한 테스트 도우미를 만드는 것이 유용 할 것입니다.