Ale*_*kov 8 mocking acceptance-testing end-to-end angularjs karma-runner
有没有人知道如何在角度e2e测试中模拟$ httpBackend?这个想法是在travis-ci上运行测试时存根XHR请求.我正在使用业力来代理我在travis上运行的rails应用程序中的资产和部分资源.我想在没有真正的DB查询的情况下进行验收测试.
这是我的业力配置文件的一部分:
...
files = [
MOCHA,
MOCHA_ADAPTER,
'spec/javascripts/support/angular-scenario.js',
ANGULAR_SCENARIO_ADAPTER,
'spec/javascripts/support/angular-mocks.js',
'spec/javascripts/e2e/**/*_spec.*'
];
...
proxies = {
'/app': 'http://localhost:3000/',
'/assets': 'http://localhost:3000/assets/'
};
...
Run Code Online (Sandbox Code Playgroud)
这是我的spec文件的一部分:
beforeEach(inject(function($injector){
browser().navigateTo('/app');
}));
it('should do smth', inject(function($rootScope, $injector){
input('<model name>').enter('smth');
//this is the point where I want to stub real http query
pause();
}));
Run Code Online (Sandbox Code Playgroud)
我试图通过$ injector接收$ httpBackend服务:
$injector.get('$httpBackend')
Run Code Online (Sandbox Code Playgroud)
但这不是在我的测试运行的iframe中使用的那个.
我做的下一次尝试是使用angular.scenario.dsl,这里是代码samle:
angular.scenario.dsl('mockHttpGet', function(){
return function(path, fakeResponse){
return this.addFutureAction("Mocking response", function($window, $document, done) {
// I have access to window and document instances
// from iframe where my tests run here
var $httpBackend = $document.injector().get(['$httpBackend']);
$httpBackend.expectGET(path).respond(fakeResponse)
done(null);
});
};
});
Run Code Online (Sandbox Code Playgroud)
用法示例:
it('should do smth', inject(function($rootScope, $injector){
mockHttpGet('<path>', { /* fake data */ });
input('search.name').enter('mow');
pause();
}));
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
<$httpBackend listing> has no method 'expectGET'
Run Code Online (Sandbox Code Playgroud)
所以,在这一点上我不知道下一步.有没有人试过做这样的事情,这种类型的存根真的可能吗?
如果你真的想在E2E测试中模拟后端(这些测试被称为场景,而Specs用于单元测试)那么这就是我在之前工作的项目中所做的.
我正在测试的应用程序被调用studentsApp.这是一个通过查询REST api来搜索学生的应用程序.我想测试应用程序而不实际查询api.
我创建了一个端到端的应用程序调用studentsAppDev我注入studentsApp和ngMockE2E成.在那里,我定义了mockBackend应该期望的调用以及要返回的数据.以下是我的studentsAppDev文件示例:
"use strict";
// This application is to mock out the backend.
var studentsAppDev = angular.module('studentsAppDev', ['studentsApp', 'ngMockE2E']);
studentsAppDev.run(function ($httpBackend) {
// Allow all calls not to the API to pass through normally
$httpBackend.whenGET('students/index.html').passThrough();
var baseApiUrl = 'http://localhost:19357/api/v1/';
var axelStudent = {
Education: [{...}],
Person: {...}
};
var femaleStudent = {
Education: [{...}],
Person: {...}
};
$httpBackend.whenGET(baseApiUrl + 'students/?searchString=axe&')
.respond([axelStudent, femaleStudent]);
$httpBackend.whenGET(baseApiUrl + 'students/?searchString=axel&')
.respond([axelStudent, femaleStudent]);
$httpBackend.whenGET(baseApiUrl + 'students/?searchString=axe&department=1&')
.respond([axelStudent]);
$httpBackend.whenGET(baseApiUrl + 'students/?searchString=axe&department=2&')
.respond([femaleStudent]);
$httpBackend.whenGET(baseApiUrl + 'students/?searchString=axe&department=3&')
.respond([]);
...
$httpBackend.whenGET(baseApiUrl + 'departments/?teachingOnly=true')
.respond([...]);
$httpBackend.whenGET(baseApiUrl + 'majors?organization=RU').respond([...]);
});
Run Code Online (Sandbox Code Playgroud)
然后,我在Jenkins CI服务器中第一步替换studentsAppwith studentsAppDev并angular-mocks.js在主index.html文件中添加引用.
小智 0
这感觉更像是单元/规范测试。一般来说,您应该在单元/规范测试中使用模拟,而不是 e2e/集成测试。基本上,将 e2e 测试视为对大多数集成应用程序的期望……模拟事物有点违背了 e2e 测试的目的。事实上,我不确定 karam 如何将 angular-mocks.js 插入正在运行的应用程序中。
规格测试可能看起来像......
describe('Controller: MainCtrl', function () {
'use strict';
beforeEach(module('App.main-ctrl'));
var MainCtrl,
scope,
$httpBackend;
beforeEach(inject(function ($controller, $rootScope, $injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.when('GET', '/search/mow').respond([
{}
]);
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should search for mow', function () {
scope.search = 'mow';
$httpBackend.flush();
expect(scope.accounts.length).toBe(1);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8333 次 |
| 最近记录: |