【问题标题】:How can I mock an Angular service using Protractor?如何使用 Protractor 模拟 Angular 服务?
【发布时间】:2017-02-14 12:16:33
【问题描述】:

我有一些用 Protractor 编写的 e2e 测试。 我的一些测试执行的东西会导致显示警报(uib-alert)。 测试运行良好,可以找到并验证警报,但如果我使用来自 uib-alert 的标签dismiss-on-timeout,测试会失败,因为量角器在执行断言之前等待超时。

问题:https://github.com/angular-ui/bootstrap/pull/2798https://github.com/angular/protractor/issues/169

所以,我想在运行量角器时创建没有超时关闭标签的警报。

我的警报是由这样的服务创建的:

angular.module('MyApp').factory("AlertService", function (...

如何在 Protractor 上覆盖它?我正在尝试使用 browser.addMockModule('AlertService', mock) 但这不起作用...

【问题讨论】:

    标签: javascript angularjs protractor


    【解决方案1】:

    我通过将 AlertService 提取到一个全新的模块然后模拟它来实现它。 我猜量角器不能模拟模块内的单个服务,只能模拟整个事情。

    编辑: 我创建了 alert.module.js:

    angular.module('MyApp.alert', []);
    angular.module('MyApp.alert').factory("AlertService", function($rootScope) {
    ...
    

    还有他们,在 protractor.conf.js 上:

    exports.config = {
      framework: 'jasmine',
      seleniumAddress: 'http://localhost:4444/wd/hub',
      specs: [],
      capabilities: {
        browserName: 'phantomjs',
        'phantomjs.binary.path': require('phantomjs-prebuilt').path
      },
      onPrepare: function() {
      ...
      function mockAlertService(){
        var alertMock = function() {
         angular.module('MyApp.alert', []);
         angular.module('MyApp.alert').factory("AlertService", function($rootScope) {
         ...
       }
       browser.addMockModule('MyApp.alert', alertMock);
      }
    

    【讨论】:

    • 您介意为此分享代码示例吗?我很想看看你是如何解决这个问题的。
    猜你喜欢
    • 2019-09-25
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2020-02-14
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    相关资源
    最近更新 更多