I'm creating a simple unit test for a service that has a dependency. It is a simple service that I am starting on to help to get my head around the best process and structure.
The unit test I have created works but TypeScript throws an error.
Argument of type '{ order_id: string; user: string; }' is not assignable to parameter of type 'Expected<Observable<any>>'.
Object literal may only specify known properties, and 'order_id' does not exist in type 'Expected<Observable<any>>'.
The service executes the method sendBody on the injected service which makes a HTTP post request. I understand an Observable should be returned here but I would have thought the mock service should have resolved this TypeScript type error.
Is there a better way to inject and mock dependencies when testing services only?
Service
export class ReturnOrderService {
constructor(private _ReturnOrderResource: ReturnOrderResource) { }
updateOrderStatus(orderId: string, user: string) {
let body = {};
if (orderId !== undefined && user !== undefined) {
body["order_id"] = orderId;
body["user"] = user;
}
return this._ReturnOrderResource.sendBody(body);
}
}
Spec
const MockReturnOrderResource = {
sendBody: function(body) {
return body;
}
}
describe('ReturnsComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({ providers: [
ReturnOrderService,
{ provide: ReturnOrderResource, useValue: MockReturnOrderResource }
] });
});
it('#updateOrderStatus should create an object literal from arguments',
async(inject([ReturnOrderService], (service: ReturnOrderService) => {
expect(service.updateOrderStatus("10006886","Mr Bungle")).toEqual({ order_id: '10006886', user: 'Mr Bungle' });
})));
});
_ReturnOrderResource.sendBodyreturn? Can you add that code as well?