16
이것은 흥미로운 문제입니다. Ionic BarcodeScanner를 사용하는 서비스를 테스트하려고합니다. 테스트를 시도하기 위해 ionic unit-testing repository을 기반으로 한 리포가 있습니다. BarcodeScanner.scan 메서드를 조롱했습니다. spyOn(..).and.callFake(..)
Angular 2 Services에서 비동기 (Promise) 메서드를 테스트하는 중
문제 : 구성 요소에서 검색 방법을 호출 할 때 작동합니다. 내가 서비스에서 똑같은 일을 할 때 시간 초과가 발생합니다.
구성 요소 테스트 코드 :
it("should be able to set a spy on the scanner and test the component", done => {
const testBC = "123456";
const spy = spyOn(TestBed.get(BarcodeScanner), "scan");
spy.and.callFake(() => {
return new Promise((resolve, reject) => {
resolve(testBC);
})
});
component.testScanner().then(res => {
expect(res).toBe(testBC);
done();
}, reason => {
expect(true).toBe(false);
done();
})
});
서비스 테스트 코드 : 그런 식으로 2 각도에서 테스트 서비스의 알려진 문제가
it("should be able to set a spy on the scanner and test the service", done => {
const testBC = "123456";
const spy = spyOn(TestBed.get(BarcodeScanner), "scan");
spy.and.callFake(() => {
return new Promise((resolve, reject) => {
resolve(testBC);
})
});
inject([TestService], (service) => {
service.testScanner().then(res => {
expect(res).not.toBe(testBC);
done()
}, reason => {
expect(true).toBe(false);
done();
})
})
});
있습니까? 어떤 도움을 주셔서 감사합니다!