Angular Jasmine/Karma测试服务与params
我想用Jasmine&Karma测试一个与TestBed或SpyOn相关的服务。但我在Angular网站上找不到我如何做到这一点的例子:Angular Jasmine/Karma测试服务与params,angular,karma-jasmine,Angular,Karma Jasmine,我想用Jasmine&Karma测试一个与TestBed或SpyOn相关的服务。但我在Angular网站上找不到我如何做到这一点的例子: 我该怎么做呢?这里有一个关于如何开始测试服务的简单示例。在大多数情况下,您不需要使用测试床,因此我在这个最小的单元测试示例中忽略了这一点: Abc服务: import { Injectable } from '@angular/core'; @Injectable() export class AbcService { constructor(priv
我该怎么做呢?这里有一个关于如何开始测试服务的简单示例。在大多数情况下,您不需要使用测试床,因此我在这个最小的单元测试示例中忽略了这一点: 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服务规范文件:
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', () => {
});
});