ジャスミンでテストしたい&カルマにはTestBedまたはSpyOnによるサービス依存関係があります。しかし、Angularサイトでどのようにできるのかわかりませんでした。https://angular.io/guide/testingジャスミン/カルマテストサービス(パラメタ付き)
どうすればいいですか?
ジャスミンでテストしたい&カルマにはTestBedまたはSpyOnによるサービス依存関係があります。しかし、Angularサイトでどのようにできるのかわかりませんでした。https://angular.io/guide/testingジャスミン/カルマテストサービス(パラメタ付き)
どうすればいいですか?
ここでは、テストサービスを開始するための最小限の例を示します。ほとんどの状況では、あなたはテストベッドを使用する必要はありませんので、私は、この最小単位の試験例の外にいることを左:
Abcのサービス:
import { Injectable } from '@angular/core';
@Injectable()
export class AbcService {
constructor(private otherService) {
}
add(x: number, y: number) {
return x + y;
}
submit(x) {
this.otherService.submit(x);
}
}
Abcのサービスspecファイル:
import { AbcService } from './abc';
describe('AbcService',() => {
it('should add 1 + 2',() => {
// Arrange
const sut = new AbcService(null);
// Act
const result = sut.add(1, 2);
// Assert
expect(result).toEqual(3);
});
it('should dispatch SET_INTERACTION when setAnswer is run',() => {
// Arrange
const mockOtherService = jasmine.createSpyObj('mockOtherService', ['submit']);
const sut = new AbcService(mockOtherService);
// Act
sut.submit(10);
// Assert
expect(mockOtherService.submit).toHaveBeenCalledWith(10);
});
});
describe('Test Service',() => {
let yourCurrentService: YouCurrentService;
let yourServiceDependencyService : YourServiceDependencyService;
beforeEach(() => {
let injector = ReflectiveInjector.resolveAndCreate([
YouCurrentService,
YourServiceDependencyService
]);
yourCurrentService = injector.get(YourServiceDependencyService);
yourCurrentService = injector.get(YouCurrentService);
});
it('test',() => {
});
});
すばらしい、ありがとう –