Angular 5 (Karma / Jasmine Testing) - Using a mock backend to mock error responses for error messages

GPD21352

I don't quite understand how the MockBackend feature works as described over here. I am completely new to Angular.

I want to program some test cases to make sure that when a server error occurs in the backend, the Angular components I have do display error messages on the frontend. That requires having HTTP requests that return errors on purpose. I am running into trouble with the jasmine-ajax and the nock packages, but someone told me about programming an instance

Even if you have an actual backend already implemented, can you still create a mock backend to test appearance of form messages which do require HTTP requests first before they are displayed?

One example is when the user is asked to fill out a sales form, and upon sending it over, the server encounters an error and sends a 500 Internal Server Error response back, which the frontend then displays a server error message.

I don't quite understand mock backends in Angular. My worry is that by having a mock backend, it will cause all the requests I'm making in the controllers of the components, as well as the requests made in the test cases, be issued to the mock backend instead of the actual backend, thereby introducing false-positive fails in my test suite.

If I program my test suite to use the MockBackend class from '@angular/http/testing', will this cause my HTTP requests on all the Angular components to be issued to that mock backend server instead of the actual server?

EDIT: This is a case of what I'm referring to. By using MockBackend, some of the tests that require sending HTTP requests to the actual server will not be sent.

qwrtln

Correct me if I'm wrong, judging by your description I assume you're using http module. You could consider upgrading to the httpClient module, for which testing against server errors is pretty straightforward: https://angular.io/guide/http#testing-for-errors

By the way unit tests should be self contained. You should mock all the dependencies (probably providers for your testing module) and not send any requests to the actual server. Both information and status codes can be mocked using http testing module.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Angular 5 testing, how to mock an http error?

Angular Karma Jasmine Testing form validation error

Jasmine mock chained methods with Karma and Angular

unable to mock HttpClient in Jasmine and Karma using HttpTestController

Angular testing using Jasmine and Karma

Karma + Jasmine (Angular Testing): Where should I define mock classes and test variables?

jasmine testing a mock service in an angular 1.5 controller

Angular 6 testing - Jasmine - mock chained promises

Getting error in controller while unit testing using karma jasmine

Problems setting up mock services and calling it with jasmine/ karma and Angular

How to mock router.navigateByUrl(...).then in Angular with Karma and Jasmine

How do I inject a mock dependency into an angular directive with Jasmine on Karma

How to mock Angular 4.3 httpClient an error response in testing

how to mock http error for angular2 testing

Mock Api Responses in Android Testing

Unit Test error for Angular using Karma / Jasmine for Router

Using Angular Mock Backend resource multiple times

AngularJS testing : Jasmine mock callback

Mock an if/else statement with jasmine - unit testing with Angular/Typescript

Error at karma testing an Angular component using mat-select and formControlName

Mock a provider class to return response and handle promise - Unit Testing with Jasmine and Karma with Ionic 3

Angular Testing with Karma Jasmine Failed

Correctly using httptest to mock responses

Testing constructor using mock

How to test If Else block in Angular Unit Testing using Jasmine /Karma

angular + jasmine + mock $stateParams in a directive

Mock 'window' object in Jasmine + Angular

Mock Angular Material MatCheckboxChanged Jasmine

How to mock dom element from karma testing

TOP Ranking

HotTag

Archive