반응형
Jasmine 2.0 async done() 및 angular-mocks inject()가 같은 테스트 it()에서 사용됩니다.
제가 평소에 사용하는 테스트 케이스는
it("should send get request", inject(function(someServices) {
//some test
}));
재스민 2.0 비동기 테스트는
it("should send get request", function(done) {
someAsync.then(function(){
done();
});
});
어떻게 하면 done과 injection을 한 번에 사용할 수 있나요?
이거면 될 거예요.재스민 2.0으로 업데이트 했을 때도 같은 문제가 발생했어요.
it("should send get request", function(done) {
inject(function(someServices) {
//some async test
done();
})(); // function returned by 'inject' has to be invoked
});
중요한 주의사항은 다음 괄호입니다.inject전화해요. 예를 들어봐요.
inject(function(someServices) {
//some async test
done();
})(); <-- these brackets here important.
의 종류를 보면inject:
export declare function inject(tokens: any[], fn: Function): () => any;
함수를 반환하는 것을 알 수 있습니다.그래서 함수를 호출하는 것을 잊어버렸기 때문에 출력을 얻을 수 없었습니다!!
생각해보면 함수를 반환하는 것은 의미가 있습니다.왜냐하면it함수를 가져간다!
따라서 추가 괄호로 모든 문제를 해결할 수 있습니다!
작업 예:
it('should allow you to observe for changes', function(done) {
inject([GlobalStateService], (globalStateService: GlobalStateService) => {
globalStateService.observe("user", storageType.InMemoryStorage, (user: string) => {
expect(user).toBe("bla");
done();
});
globalStateService.write({ user: "bla"}, storageType.InMemoryStorage);
})();
});
@Scott Borling의 답변과 @White Angel의 코멘트에 덧붙여, 주입내의 코드가 호출되지 않았다고 하는 코멘트를 추가합니다.
이 방법은 효과가 있었습니다.
it("should send get request", function(done) {
inject(function(someServices) {
//some async test
done();
})();
});
다음과 같이 시험을 작성할 수 있습니다.
describe("Some service'", function () {
var service;
var data;
beforeEach(function (done) {
module('app');
inject(function (someService) {
service = someService;
});
service
.getData()
.then(function(result) {
data = result;
done();
});
});
it('should return a result', function () {
expect(data).toBeDefined();
});
}
Angular 5.2.0의 경우: @scott-boring의 접근방식은 나에게 효과가 없었습니다.작동한 것은 이 기능을 사용하여TestBed.get()서비스를 받다inject()매뉴얼에 기재되어 있는 바와 같이
describe('TooltipComponent', () => {
let component: TooltipComponent;
let fixture: ComponentFixture<TooltipComponent>;
let myService: MyService;
beforeEach(async(() => {
const myServiceSpy = jasmine.createSpyObj('MyService', ['calc']);
TestBed.configureTestingModule({
declarations: [ MyComponent ],
providers: [
{provide: MyService, useValue: myServiceSpy}
]
})
.compileComponents();
myService = TestBed.get(MyService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should render correctly', (done) => {
component.render();
setTimeout(() => {
expect(myService.calc).toHaveBeenCalled();
done();
}, 1000);
});
언급URL : https://stackoverflow.com/questions/25274152/jasmine-2-0-async-done-and-angular-mocks-inject-in-same-test-it
반응형
'programing' 카테고리의 다른 글
| Gson의 POJO 어레이 리스트 시리얼화 문제 (0) | 2023.03.20 |
|---|---|
| 컨트롤러에서 $inject를 사용할 수 없습니다. (0) | 2023.03.20 |
| ng하위 수에 따른 숨김 또는 ngShow (0) | 2023.03.20 |
| woocommerce 브레드 크럼 누락 숍 링크 (0) | 2023.03.20 |
| MongoDB에서 데이터베이스 간에 컬렉션을 복사하는 방법 (0) | 2023.03.20 |