【问题标题】:Using ng-describe for end-to-end testing with protractor使用 ng-describe 进行带量角器的端到端测试
【发布时间】:2015-09-01 04:49:15
【问题描述】:

我最近发现了一个很棒的 ng-describe 包,它通过抽象出所有你必须记住/查找和编写以便加载、注入、模拟或间谍。

有人尝试将ng-describeprotractor 一起使用吗?这有意义吗?我们能从中受益吗?


引起我注意的一件事是您可以轻松地模拟 HTTP 响应:

ngDescribe({
  inject: '$http', // for making test calls
  http: {
    get: {
      '/my/url': 42, // status 200, data 42
      '/my/other/url': [202, 42], // status 202, data 42,
      '/my/smart/url': function (method, url, data, headers) {
        return [500, 'something is wrong'];
      } // status 500, data "something is wrong"
    }, 
    post: {
      // same format as GET
    }
  },
  tests: function (deps) {
    it('responds', function (done) {
      deps.$http.get('/my/other/url')
        .then(function (response) {
          // response.status = 202
          // response.data = 42
          done();
        });
      http.flush();
    });
  }
});

模拟 HTTP 响应通常有助于实现更好的 e2e 覆盖并测试 UI 如何对特定情况做出反应以及错误处理如何工作。这是我们目前正在使用protractor-http-mock 做的事情,还有other options 看起来不像ng-describe 那样容易。

【问题讨论】:

    标签: javascript angularjs testing protractor ngdescribe


    【解决方案1】:

    Protractor primary 用于 E2E 测试(使用 selenium webdriver),这意味着您需要连接一个实际的后端(它也可以是一个模拟后端)。正如 Protractor 的创建者所写的here,您的应用程序代码与测试代码分开运行,并且无法轻松访问 $http 服务。

    通过模拟后端调用,即使您使用 Protractor 等 E2E 测试工具,您也不再进行 E2E 测试。那么为什么不回到单元测试呢。唯一的区别是您将使用 jQuery 而不是 Protractor API,并且测试将使用 Karma 运行。然后您可以轻松地使用 ng-describe$httpBackend,它们主要用于单元测试。

    但是,如果您想继续使用这种方法,您可以查看Protractor issue 中的 cmets。有几个人正在为这个问题提出解决方案,如前所述,您已经在使用其中一个。但在这种情况下,ng-describe 对你没有多大帮助。

    我希望这能回答你的问题。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2014-08-04
      • 2017-11-30
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多