我正在尝试为使用 nswag 生成的客户端编写一个简单的单元测试,它看起来
it('it create a call create on the api if new household', async(() => {
const newHousehold = new Household();
newHousehold.name = 'Test Household';
store.dispatch(new SaveHousehold(newHousehold));
const req = httpTestingController.expectOne('http://localhost:57461/api/Households');
req.flush(newHousehold);
expect(req.request.method).toEqual('POST');
}));
但我收到以下错误:
Failed: Automatic conversion to Blob is not supported for response type
我尝试了几种方法将其转换newHousehold
为 blob,但它总是返回空白或引发其他错误。那么我怎样才能测试这个客户端呢?
仅供参考,我也将 NGXS 用于商店,这就是为什么它调用触发 http 请求的 store.dispatch 的原因。
这是操作的代码
@Action(SaveHousehold)
public saveHousehold(
{ dispatch }: StateContext<HouseholdStateModel>,
payload: SaveHousehold
) {
let subscription: Observable<Household>;
if (payload.household.id) {
subscription = this.service.update(
payload.household.id,
payload.household
);
} else {
subscription = this.service.create(payload.household);
}
subscription.subscribe(result => {
if (!payload.household.id) {
dispatch(new LinkToExistingHousehold(result.id));
}
},error=>{
console.error(error);
});
}
这是代码service.create
create(entity: Household): Observable<Household | null> {
let url_ = this.baseUrl + "/api/Households";
url_ = url_.replace(/[?&]$/, "");
const content_ = JSON.stringify(entity);
let options_ : any = {
body: content_,
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request("post", url_, options_).pipe(_observableMergeMap((response_ : any) => {
return this.processCreate(response_);
})).pipe(_observableCatch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.processCreate(<any>response_);
} catch (e) {
return <Observable<Household | null>><any>_observableThrow(e);
}
} else
return <Observable<Household | null>><any>_observableThrow(response_);
}));
}
这个问题似乎是因为responseType
在创建中指定。