使用ng-describe进行量角器的端到端测试

时间:2015-09-01 04:49:16

标签: javascript angularjs testing protractor ngdescribe

我最近发现了一个很棒的ng-describe包,它通过抽象掉你必须记住/查找和写入的所有样板代码来加载,注入,使AngularJS应用程序的单元测试非常透明。嘲笑或间谍。

有人试图将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 optionsng-describe一样简单。

1 个答案:

答案 0 :(得分:9)

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

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

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

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

相关问题