我遇到的问题是我创建了一个Injectable
服务类,其中包含静态方法,如:
declare let Granite: any;
@Injectable()
export class I18NService {
public static get(key:string):string {
return Granite.I18n.get(key);
}
}
我正在尝试使用jasmine测试它,我编写了测试用例,如:
export function main() {
fdescribe('Granite Service', () => {
class MockGraniteService {
get(st: string): string {
return '';
}
}
beforeEach(() => {
//spyOn(I18NService, 'get').and.callThrough();
TestBed.configureTestingModule({
providers: [{
provide: I18NService,
useClass: MockGraniteService
}]
});
});
it('should be granite processed value from Granite API', () => {
//Spy
spyOn(I18NService, 'get').and.callFake(() => {
return "Ship To";
});
let processedVal = I18NService.get('mkt.header.shipToLabel');
expect(processedVal).toBeTruthy();
expect(processedVal).toEqual('Ship To');
});
})
}
当我在监视get()
时,它被排除在代码覆盖范围之外,当我使用callThrough()
直接调用该函数时,它正在进行实际调用但未获得花岗岩处理值。有什么帮助吗?