ale*_*cxe 10 javascript testing angularjs protractor ngdescribe
我最近发现了一个很棒的ng-describe软件包,它通过抽象掉你必须记住/查找和编写的所有样板代码来加载,注入,模拟或间谍,从而使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();
});
}
});
Run Code Online (Sandbox Code Playgroud)
模拟HTTP响应通常有助于实现更好的e2e覆盖率,并测试UI如何对特定情况做出反应以及错误处理如何工作.这是我们目前正在做的事情protractor-http-mock,还有其他选择看起来不像现在这么简单ng-describe.
Protractor primary用于E2E测试(使用selenium webdriver),这意味着你需要连接一个实际的后端(它也可以是一个模拟后端).正如Protractor的创建者在此处所写,您的应用程序代码与测试代码分开运行,并且无法轻松访问$ http服务.
通过模拟后端调用,即使您正在使用Protractor等E2E测试工具,也不再进行E2E测试.那么为什么不回到单元测试呢.唯一的区别是你将使用jQuery而不是Protractor API,测试将与Karma一起运行.然后,您可以轻松地使用ng-describe和$ httpBackend,这些主要用于单元测试.
但是,如果您想继续使用此方法,可以查看此Protractor问题中的注释.有几个人正在为这个问题提出解决方案,如上所述,你已经在使用其中一个.但在这种情况下,ng-describe对你没有多大帮助.
我希望这能回答你的问题.