【发布时间】:2022-06-10 21:22:25
【问题描述】:
我想测试是否在 http 服务层中调用了我的辅助函数之一,但测试失败。我是开玩笑的新手,所以请告诉我我做错了什么
服务层
public customerUpload(
file: Blob,
name?: string): Observable<CustomerResponse> {
if (name!== '') {
parameters = addQueryPara(name, 'name');
}
return this.http.post(file, parameters)
)
我想检查我是否使用名称调用了 CustomerUpload,它应该调用 addQueryPara
我的规格文件测试
import * as helper from 'app/shared/helper.ts';
describe('customerService', () => {
let service: customerService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule],
});
service = TestBed.inject(customerService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
describe('when customer is called', () => {
beforeEach(() => {
const response = getMockResponse();
jest.spyOn(service, 'customerUpload').mockReturnValue(of(response) as any);
});
it('should add http params', () => {
service.customerUpload(new Blob(), 'learn');
expect(jest.spyOn(helper, 'addQueryPara')).toHaveBeenCalledTimes(1); // This is failing
});
});
});
addQueryPara 失败。如何确定我是否传递了一个参数,它调用了 addQueryPara?
【问题讨论】:
标签: angular unit-testing testing jestjs