2017-05-11 3 views
2

NativeScript Angular 2 애플리케이션에서 매우 간단한 구성 요소 테스트를 수행하는 데 문제가 있습니다. 나는 그냥 "new Component()"를 호출 한 다음 this 테스트 예제에 표시된 것처럼 테스트하지 못하는 것 같습니다. 구성 요소에 액세스하기 위해 Angular 2 TestBed 구현을 시도하고 있습니다. 이것도 작동하지 않는 것 같습니다. 아무도 비슷한 문제에 부딪 혔거나 이와 관련된 경험이 있습니까? 시험NativeScript Angular2 앱의 테스트 구성 요소

구성 요소 :

import { Component } from "@angular/core"; 
@Component({ 
    selector: "links", 
    templateUrl: "./components/links/links.component.html" 
}) 

export class LinksComponent { 
    test = "test"; 

    public constructor() { 

    } 
} 

테스트 :

import "reflect-metadata"; 
import { LinksComponent } from "../../components/links/links.component"; 
import { ComponentFixture, TestBed } from '@angular/core/testing'; 

describe("Links Component",() => { 

    let comp: LinksComponent; 
    let fixture: ComponentFixture<LinksComponent>; 

    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ LinksComponent ], 
     }); 

     fixture = TestBed.createComponent(LinksComponent); 

     comp = fixture.componentInstance; 
    }); 

    it("will pass because this is a silly test",() => { 
     expect(comp.test).toEqual("test"); 
    }) 
}); 

로그 출력 :

NativeScript/10.3 (10.3; iPhone) ../../tests/components/links.spec.js at line 0 FAILED 
ReferenceError: Can't find variable: Zone 
NativeScript/10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) (0 secs/0 seNativeScript/10.3 (10.3; iPhone): Executed 1 of 0 (1 FAILED) ERROR (0.008 secs/0 secs) 
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:258:24: NSUTR: completeAck 
May 11 16:16:43 --- last message repeated 1 time --- 
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:90:28: NSUTR-socket.io: io server disconnect 
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:151:24: NSUTR: disregarding second execution 
Test run failed. 
+2

아직 공식적으로 지원되지는 않지만 커뮤니티 회원이 해결할 수있는 방법에 대해서는 링크 된 문제의 토론을 참조하십시오. https://github.com/NativeScript/nativescript-angular/issues/479#issuecomment-265966999 –

답변

2

AFAIK 테스트 베드가 NativeScript 각도에서 작동하지 않습니다. new으로 인스턴스화해야하는 구성 요소를 모의 해 각 종속성을 해결하십시오.

repository이 NativeScript에 대한 몇 가지 단위 테스트를 할 수 있기를 공부해야한다.

+0

정확합니다. . 나는 몇몇 사용자가 그것을 작동하게 만들었다는 것을 알았지 만 이것은 더 나은 접근법처럼 보인다. 처음에는이 작업을 시도하고 karma.config에 TypeScript 파일 만 지정했습니다. 화면에 오류가 가득 찼습니다. TypeScript 파일을 제거하고 transpiled JS 파일을 추가하자 마자 테스트가 제대로 실행됩니다. 저를 올바른 방향으로 가리켜 주셔서 감사합니다! – jab88