Reputation: 35
Is it possible to force a specific error in angular for testing purposes using RXJS?
Example:
somerequest('http://myserversomewhere/endpoint').pipe(new Error(404, 'Endpoint Not Found'));
Instead of setting up a mock server somewhere and building out the whole set of errors to test
Upvotes: 0
Views: 94
Reputation: 5313
Yes, you can mock an error, but first you should mock your service provider. Here an example:
import { throwError } from 'rxjs';
describe('Set of tests', () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
providers: [
{ provide: MyService, useClass: FakeMyService }
],
}).compileComponents();
}));
it('My first test', () => {
myService = TestBed.get(MyService);
// HERE IS WHERE WE SHOULD MOCK A SERVICE' FUNCTION
spyOn(myService, 'myServiceFunction')
.and.returnValue(throwError(new Error(404, 'Endpoint Not Found')));
});
});
Full example on github: here
Upvotes: 1