使用 ng-describe 与量角器进行端到端测试
Using ng-describe for end-to-end testing with protractor
我最近发现了一个很棒的 ng-describe
包,它通过抽象掉所有必须 remember/look 的样板代码,使为 AngularJS 应用程序编写单元测试变得非常透明编写以加载、注入、模拟或监视。
有人试过将 ng-describe
与 protractor
一起使用吗?它有意义吗?我们可以从中受益吗?
引起我注意的一件事是您可以轻松模拟 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 响应通常有助于实现更好的端到端覆盖并测试 UI 如何对特定情况做出反应以及错误处理如何工作。这是我们目前正在用 protractor-http-mock
, there are also other options 做的事情,它看起来不像 ng-describe
.
那样容易
Protractor 主要用于 E2E 测试(使用 selenium webdriver),这意味着您需要连接一个实际的后端(它也可以是一个模拟后端)。正如 Protractor 的创建者所写 here,您的应用程序代码 运行 与测试代码分开,不可能轻松访问 $http 服务。
通过模拟后端调用,即使您使用 Protractor 等 E2E 测试工具,您也不再进行 E2E 测试。为什么不return 进行单元测试呢。唯一的区别是您将使用 jQuery 而不是量角器 API 并且测试将是 运行 with Karma。然后你可以轻松地使用 ng-describe 和 $httpBackend 主要用于单元测试。
但是,如果您想继续使用这种方法,可以查看 Protractor issue 中的评论。有几个人正在为这个问题提出解决方案,并且如前所述,您已经在使用其中一个。但在这种情况下 ng-describe 对你帮助不大。
我希望这能回答您的问题。
我最近发现了一个很棒的 ng-describe
包,它通过抽象掉所有必须 remember/look 的样板代码,使为 AngularJS 应用程序编写单元测试变得非常透明编写以加载、注入、模拟或监视。
有人试过将 ng-describe
与 protractor
一起使用吗?它有意义吗?我们可以从中受益吗?
引起我注意的一件事是您可以轻松模拟 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 响应通常有助于实现更好的端到端覆盖并测试 UI 如何对特定情况做出反应以及错误处理如何工作。这是我们目前正在用 protractor-http-mock
, there are also other options 做的事情,它看起来不像 ng-describe
.
Protractor 主要用于 E2E 测试(使用 selenium webdriver),这意味着您需要连接一个实际的后端(它也可以是一个模拟后端)。正如 Protractor 的创建者所写 here,您的应用程序代码 运行 与测试代码分开,不可能轻松访问 $http 服务。
通过模拟后端调用,即使您使用 Protractor 等 E2E 测试工具,您也不再进行 E2E 测试。为什么不return 进行单元测试呢。唯一的区别是您将使用 jQuery 而不是量角器 API 并且测试将是 运行 with Karma。然后你可以轻松地使用 ng-describe 和 $httpBackend 主要用于单元测试。
但是,如果您想继续使用这种方法,可以查看 Protractor issue 中的评论。有几个人正在为这个问题提出解决方案,并且如前所述,您已经在使用其中一个。但在这种情况下 ng-describe 对你帮助不大。
我希望这能回答您的问题。