반응형
Jasmine 2.0 비동기 done () 및 angular-mocks inject () 동일한 테스트 it ()
내 일반적인 테스트 케이스는 다음과 같습니다.
it("should send get request", inject(function(someServices) {
//some test
}));
그리고 Jasmine 2.0 비동기 테스트는 다음과 같아야합니다.
it("should send get request", function(done) {
someAsync.then(function(){
done();
});
});
done과 injection을 하나의 테스트에서 어떻게 사용할 수 있습니까?
작동합니다. Jasmine 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 Boring의 답변과 인 젝트 내부의 코드가 호출되지 않았다고 언급 한 @WhiteAngel의 의견에 추가합니다.
이것은 나를 위해 일했습니다.
it("should send get request", function(done) {
inject(function(someServices) {
//some async test
done();
})();
});
You could write the test like that:
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();
});
}
For Angular 5.2.0: @scott-boring's approach did not work for me. What did work is using the TestBed.get()
to get the services instead of inject()
, as described in the docs:
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);
});
반응형
'developer tip' 카테고리의 다른 글
Ajax 업로드 이미지 (0) | 2020.12.08 |
---|---|
StackOverflowException이 throw 될 때 .NET이 그렇게 제대로 작동하지 않는 이유는 무엇입니까? (0) | 2020.12.08 |
도커 이미지가 도커에서 사용하지 않는 디스크 공간을 차지하는 이유 (0) | 2020.12.08 |
HEAD 기록에서 업스트림 SVN 정보를 확인할 수 없습니다. (0) | 2020.12.08 |
std :: function의 성능 오버 헤드는 무엇입니까? (0) | 2020.12.08 |