I am trying to write a simple unit test for a client that was generated with nswag and it look
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');
}));
But I get the following error:
Failed: Automatic conversion to Blob is not supported for response type
I have tried several things to convert the newHousehold
to a blob but it always returns blank or throws other errors. So how can I can I test this client?
FYI, I am also using NGXS for a store, so that is why it is calling the store.dispatch which fires the http request.
Here is the code for the action
@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);
});
}
And here is the code for the 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_);
}));
}
The issue seems to be because the responseType
specified in the create.