在异步函数中使用 ts-mockito 抛出错误
throw an error using ts-mockito in async function
我正在尝试使用 ts-mockito 测试 API 端点。事情进展顺利,直到我开始异步......
我的测试:
it('should throw an error if the address is not valid',()=>{
const mockedGeolocationService: GoogleGeolocationService = mock(GoogleGeolocationService);
when(mockedGeolocationService.getLocation(address)).thenThrow(new Error("the address provided is not valid"));
const geolocationService: IGeolocationService = instance(mockedGeolocationService);
const addressService: AddressService = new AddressService(geolocationService, new MongoAddressRepository());
expect(() => addressService.storeAddress(address)).to.throw("the address provided is not valid");
});
服务:
public storeAddress = (address: Address): void => {
const location = this.geolocationService.getLocation(address);
address.setLocation(location);
this.addressRepository.store(address);
}
到目前为止,一切正常。但是当我开始实现地理定位服务时,我不得不将其声明为一个承诺,因为它执行一个 http 请求。
public storeAddress = async (address: Address): Promise<void> => {
const location = await this.geolocationService.getLocation(address);
address.setLocation(location);
this.addressRepository.store(address);
}
那我就没法捕捉到抛出的错误了,毕竟还是抛出了...
任何线索我应该如何捕获或抛出这个错误?提前致谢。
我想出了如何处理这些错误,我留下了答案以防它能帮助到别人。问题是因为它是一个异步函数,所以它不会捕获错误。它必须手动处理并比较错误(作为字符串),如下所示:
it('should throw an error if the address is not valid',(done)=>{
const mockedGeolocationService: GoogleGeolocationService = mock(GoogleGeolocationService);
when(mockedGeolocationService.getLocation(address)).thenThrow(new Error("the address provided is not valid"));
const geolocationService: IGeolocationService = instance(mockedGeolocationService);
const addressService: AddressService = new AddressService(geolocationService, new MongoAddressRepository());
addressService.storeAddress(address).catch(error => {
expect(error.toString()).to.be.equal(new Error("the address provided is not valid").toString());
done();
});
});
我正在尝试使用 ts-mockito 测试 API 端点。事情进展顺利,直到我开始异步...... 我的测试:
it('should throw an error if the address is not valid',()=>{
const mockedGeolocationService: GoogleGeolocationService = mock(GoogleGeolocationService);
when(mockedGeolocationService.getLocation(address)).thenThrow(new Error("the address provided is not valid"));
const geolocationService: IGeolocationService = instance(mockedGeolocationService);
const addressService: AddressService = new AddressService(geolocationService, new MongoAddressRepository());
expect(() => addressService.storeAddress(address)).to.throw("the address provided is not valid");
});
服务:
public storeAddress = (address: Address): void => {
const location = this.geolocationService.getLocation(address);
address.setLocation(location);
this.addressRepository.store(address);
}
到目前为止,一切正常。但是当我开始实现地理定位服务时,我不得不将其声明为一个承诺,因为它执行一个 http 请求。
public storeAddress = async (address: Address): Promise<void> => {
const location = await this.geolocationService.getLocation(address);
address.setLocation(location);
this.addressRepository.store(address);
}
那我就没法捕捉到抛出的错误了,毕竟还是抛出了... 任何线索我应该如何捕获或抛出这个错误?提前致谢。
我想出了如何处理这些错误,我留下了答案以防它能帮助到别人。问题是因为它是一个异步函数,所以它不会捕获错误。它必须手动处理并比较错误(作为字符串),如下所示:
it('should throw an error if the address is not valid',(done)=>{
const mockedGeolocationService: GoogleGeolocationService = mock(GoogleGeolocationService);
when(mockedGeolocationService.getLocation(address)).thenThrow(new Error("the address provided is not valid"));
const geolocationService: IGeolocationService = instance(mockedGeolocationService);
const addressService: AddressService = new AddressService(geolocationService, new MongoAddressRepository());
addressService.storeAddress(address).catch(error => {
expect(error.toString()).to.be.equal(new Error("the address provided is not valid").toString());
done();
});
});