Angular 5(业力/茉莉花测试)-使用模拟后端模拟错误消息的错误响应 [英] Angular 5 (Karma / Jasmine Testing) - Using a mock backend to mock error responses for error messages

查看:97
本文介绍了Angular 5(业力/茉莉花测试)-使用模拟后端模拟错误消息的错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解MockBackend功能的工作方式,如此处所述 .我对Angular完全陌生.

我想编写一些测试用例,以确保当后端发生服务器错误时,我拥有的Angular组件确实在前端显示错误消息.这要求HTTP请求故意返回错误.我在使用jasmine-ajax和nock软件包时遇到了麻烦,但是有人告诉我有关实例编程的问题

即使您已经实现了实际的后端,您是否仍然可以创建一个模拟后端来测试表单消息的外观,这些消息在显示之前确实需要先进行HTTP请求?

一个示例是,当要求用户填写销售表格时,服务器将其发送出去时遇到错误,并向其发送500 Internal Server Error响应,然后前端会显示服务器错误消息. /p>

我不太了解Angular中的模拟后端.我担心的是,通过拥有一个模拟后端,它将导致我在组件控制器中发出的 all 请求以及测试用例中的请求被发布给模拟后端,而不是实际的后端,从而在我的测试套件中引入了假阳性失败.

如果我将测试套件编程为使用来自'@ angular/http/testing'的MockBackend类,则这将导致我将所有Angular组件上的HTTP请求发送到该模拟后端服务器,而不是实际的服务器?

这是我遇到的情况我指的是.通过使用MockBackend,某些需要将HTTP请求发送到实际服务器的测试将不会发送.

解决方案

如果我错了,请纠正我,根据您的描述,我认为您正在使用http模块.您可以考虑升级到> httpClient 模块,针对服务器错误进行测试非常简单: https://angular.io/guide/http#testing-for-errors

通过这种方式,单元测试应该是独立的.您应该模拟所有依赖项(可能是测试模块的提供程序),并且不要将任何请求发送到实际服务器.可以使用 http测试模块来模拟信息代码和状态代码.

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.

解决方案

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.

这篇关于Angular 5(业力/茉莉花测试)-使用模拟后端模拟错误消息的错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆