简体   繁体   English

角度E2E测试数据:ngMockE2E还是测试数据服务器?

[英]Angular E2E test data: ngMockE2E or test data server?

I am developing an Angular app (Angular 1.1.5). 我正在开发一个Angular应用程序(Angular 1.1.5)。 I need some way of mocking REST API responses for e2e tests. 我需要一些模拟REST API响应以进行e2e测试的方法。

AngularJS (Green & Seshadri) goes into some detail on unit testing HTTP requests with $httpBackend , but has little to say about e2e tests. AngularJS(Green&Seshadri)详细介绍了使用$httpBackend进行HTTP请求的单元测试,但是关于$httpBackend测试则无话可说。

The documentation suggests using the ngMockE2E module to mock $httpBackend (which is distinct from the normal unit testing $httpBackend mock. 该文档建议使用ngMockE2E模块来模拟$httpBackend (这与正常的单元测试$httpBackend模拟不同。

I spent a few hours investigating the ngMockE2E API and, whilst I can see some benefits, there seem to be many downsides. 我花了几个小时研究ngMockE2E API,尽管我看到了一些好处,但似乎还有很多缺点。 My question is: 我的问题是:

What are the advantages of ngMockE2E over a simple server that provides test responses? 与提供测试响应的简单服务器相比, ngMockE2E有什么优势? or, more generally, What is the best way to test an Angular app's interactions with a server? 或者,更一般而言, 什么是测试Angular应用与服务器交互的最佳方法?

I can think of a few disadvantages: 我可以想到一些缺点:

  • I need to change my HTML to bootstrap the app with a different Angular module (a new one that depends on ngMockE2E and my actual application module) 我需要更改HTML以使用其他Angular模块(依赖于ngMockE2E和我的实际应用程序模块的新模块)来引导应用程序
  • There are no HTTP requests for Firebug/Developer tools to inspect. 没有HTTP要求Firebug / Developer工具进行检查。 This is a big one for me. 对我来说,这是一个很大的机会。 I ended up writing request/response logging methods in my mock backend. 我最终在模拟后端中编写了请求/响应日志记录方法。 I guess most mock back ends contain this kind of code. 我猜大多数模拟后端都包含此类代码。
  • There isn't an easy way to extract parameters from URLs. 没有简单的方法可以从URL中提取参数。 If I wanted to the get the id parameter from /resource/:id , I'd need to give a regex to match the URL ( \\/resource\\/.+$ ), then split the matching URL string on slash and pick the last element. 如果我想从/resource/:id获取id参数,则需要提供一个正则表达式以匹配URL( \\/resource\\/.+$ ),然后在斜杠上拆分匹配的URL字符串并选择最后一个元素。
  • The API is clunky. API很笨拙。 Returning a static object is simple, but dynamically determining which test object to return based on the requested URL is difficult. 返回静态对象很简单,但是根据请求的URL动态确定要返回哪个测试对象却很困难。 Some apps may be able to hard-code a complete set of test URLs, but I need to test a large number of resources so this is impractical for me. 有些应用程序可能可以对一组完整的测试URL进行硬编码,但是我需要测试大量资源,因此这对我来说是不切实际的。 More complex code means there is more to go wrong. 更复杂的代码意味着会有更多出错的地方。

I can also think of some advantages. 我还可以想到一些优点。 When using ngMockE2E : 使用ngMockE2E

  • I don't need to tell my app where the test API is - $httpBackend will inspect all HTTP requests for matches. 我不需要告诉我的应用程序测试API的位置- $httpBackend将检查所有 HTTP请求是否匹配。
  • I don't need to run a web server at all. 我根本不需要运行Web服务器。
  • I don't need to restart a server to pick up changes to my test data. 我不需要重新启动服务器就可以对测试数据进行更改。
  • There are fewer overall components in the tests. 测试中的整体组件较少。 However, given that this is an integration test, this isn't a huge benefit. 但是,考虑到这是一个集成测试,这并不是一个巨大的好处。

Very interested in thoughts and experiences. 对思想和经验非常感兴趣。

My conclusion, several weeks of development later: I found a simple dummy backend (Node/Express) more flexible and maintanable than the e2e testing mock backend. 我的结论是,经过几周的开发:我发现一个简单的虚拟后端(Node / Express)比e2e测试模拟后端更灵活和可维护。 The most compelling reasons are proper URL pattern matching and real HTTP requests for browser debugging. 最引人注目的原因是正确的URL模式匹配和用于浏览器调试的真实HTTP请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM