用量角器模拟和存根 [英] Mocking and Stubbing with protractor

查看:32
本文介绍了用量角器模拟和存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用量角器测试我的 angular 应用程序.该应用程序有一个与服务器通信的 API 模块在这些测试中,我想模拟这个 Api 模块.我不想进行完整的集成测试,而是使用来自 API 的预期值对用户输入进行测试.这不仅可以让客户端测试更快,还可以让我测试边缘情况,比如连接错误.

I want to test my angular app with protractor. The app has an API Module that talks to the server During these tests I want to mock this Api Module. I don't want to do full integration tests, but tests from the user input with expected values from the API. Not only could this make the client tests faster, it would also allow me to test for edge cases, like connection errors.

我怎样才能用量角器做到这一点?我刚刚开始设置集成测试.

How can I do this with protractor? I just started to setup integration tests.

我使用了 npm 量角器模块,安装了 selenium,调整了默认配置并使用了 onProtractorRunner.js 来验证我的设置是否有效.

I used the npm protractor module, installed selenium, adjusted the default config and used the onProtractorRunner.js to verify my setup works.

推荐的模拟方式是什么?我认为模拟必须在浏览器中完成,而不是直接在测试文件中完成.我假设测试文件中的命令是特定于量角器的,并将发送给 selenium runner.因此,我无法在会话和测试期间共享 javascript 对象.

What is the recommended way of mocking? I assume that the mocking has to be done within the browser and not directly in the test file. I assume that the commands in the test file are protractor specific and will be sent to the selenium runners. Therefore I can't share javascript objects during the session and the test.

不知何故,我希望我需要一个像 sinon.js 这样的间谍库,或者这是否已经包含在量角器中?

I somehow expect that I will need a spy library like sinon.js or is this already included in protractor?

我在量角器问题跟踪器中阅读了这个问题,其中可能是一种方法.基本上你在测试中编写了一个模拟模块,它被发送到浏览器/应用程序范围内执行.

I read about this issue in the protractor issue tracker, which could be a way to do it. Basically you write a Mock Module in the test, that is sent to be executed in the browser/ the applications scope.

这里有更有希望的问题.第一个讨论将 Mocks 添加到 Angular 应用程序.第二个是关于模拟后端.

Here are more promising Issues. The first talks about adding Mocks to the Angular App. The second talks about mocking the backend.

这看起来非常好,在这种情况下,Angular 应用程序将保持其原始形式.但是,这目前仅适用于已弃用的 ng-scenarios.

This looks really nice, in this case the Angular App would stay in it's original form. However this currently only works with the deprecated ng-scenarios.

推荐答案

这篇博文讨论了 Protractor 的高级使用场景.特别是它涵盖了 Protractor 浏览器对象的鲜为人知的 addMockModule() 方法.该方法允许您在 Protractor 中创建角度模块(即 API 模块的模拟或存根)并将它们上传到浏览器以替换给定规范或规范集上下文中的实际实现.

This blog post discusses advance usage scenarios for Protractor. In particular it covers the the little know addMockModule() method of the Protractor browser object. The method allows you to create angular modules in Protractor (i.e. mocks or stubs of your API module) and upload them to the browser to replace the real implementation within the context of a given spec or set of specs.

这篇关于用量角器模拟和存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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