如何为 public 静态方法编写 jasmine-karma 测试用例?
How do I write jasmine-karma test cases for public static methods?
我正在为一个服务编写 jasmine-karma 测试用例,其中的方法本质上是 public 静态的。我如何无法访问它们或在 spec.ts 文件中为它们编写测试用例。谁能告诉我怎么做?
这是我在app.service.ts
中的方法
public static getModifiedDate(dateVal: any): string {
if ('' === dateVal) {
return null;
}
const year = dateVal.substring(
dateVal.lastIndexOf('/') + 1,
dateVal.length
);
const month = dateVal.substring(0, dateVal.indexOf('/'));
const date = dateVal.substring(
dateVal.indexOf('/') + 1,
dateVal.lastIndexOf('/')
);
return year + '-' + month + '-' + date;
}
在 spec.ts 文件中,我无法为这个函数编写测试用例,因为我试过,
describe('AppService', () => {
let service: AppService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [RestClientService, AppService]
});
service = TestBed.get(AppService);
}));
it('getModifieddate should return date in the expected format', () => {
const date = service.getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
});
显示错误,因为 getModifiedDate 是 public 静态方法。
解决这个问题的正确方法是什么?
您应该以静态方式调用静态方法:AppService.getModifiedDate
。测试可以简化如下:
describe('AppService', () => {
it('getModifieddate should return date in the expected format', () => {
const date = AppService.getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
});
有几项您可以尝试,这里是:
it('getModifieddate should return date in the expected format', () => {
const date = (service as any).getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
it('getModifieddate should return date in the expected format', () => {
spyOn(service as any, 'getModifiedDate').and.callFake(() => {return '2017-04-20'; });
const date = (service as any).getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
it('getModifieddate should return date in the expected format', () => {
const date = AppService.getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
希望这对您有所帮助。如果您有任何疑问,请告诉我。
我正在为一个服务编写 jasmine-karma 测试用例,其中的方法本质上是 public 静态的。我如何无法访问它们或在 spec.ts 文件中为它们编写测试用例。谁能告诉我怎么做?
这是我在app.service.ts
中的方法public static getModifiedDate(dateVal: any): string {
if ('' === dateVal) {
return null;
}
const year = dateVal.substring(
dateVal.lastIndexOf('/') + 1,
dateVal.length
);
const month = dateVal.substring(0, dateVal.indexOf('/'));
const date = dateVal.substring(
dateVal.indexOf('/') + 1,
dateVal.lastIndexOf('/')
);
return year + '-' + month + '-' + date;
}
在 spec.ts 文件中,我无法为这个函数编写测试用例,因为我试过,
describe('AppService', () => {
let service: AppService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [RestClientService, AppService]
});
service = TestBed.get(AppService);
}));
it('getModifieddate should return date in the expected format', () => {
const date = service.getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
});
显示错误,因为 getModifiedDate 是 public 静态方法。 解决这个问题的正确方法是什么?
您应该以静态方式调用静态方法:AppService.getModifiedDate
。测试可以简化如下:
describe('AppService', () => {
it('getModifieddate should return date in the expected format', () => {
const date = AppService.getModifiedDate('04/20/2017');
expect(date).toEqual('2017-04-20');
});
});
有几项您可以尝试,这里是:
it('getModifieddate should return date in the expected format', () => { const date = (service as any).getModifiedDate('04/20/2017'); expect(date).toEqual('2017-04-20'); });
it('getModifieddate should return date in the expected format', () => { spyOn(service as any, 'getModifiedDate').and.callFake(() => {return '2017-04-20'; }); const date = (service as any).getModifiedDate('04/20/2017'); expect(date).toEqual('2017-04-20'); });
it('getModifieddate should return date in the expected format', () => { const date = AppService.getModifiedDate('04/20/2017'); expect(date).toEqual('2017-04-20'); });
希望这对您有所帮助。如果您有任何疑问,请告诉我。