angular 使用 $httpBackend + $resource 的业力单元测试,无需使用模块进行引导
angular karma unit test using $httpBackend + $resource without bootstrapping with module
要求
- Angular 基于 REST 客户端组件
- 必须在私有 API 和服务
之间进行交互
- Karma/Jasmine 测试没有 bootstrap 应用程序模块
目前的困境
我们不想 bootstrap 整个应用程序进行单元测试(使用 module() 或 angular.mock.module())。尽管 Angular 的组件注册是一项有用的功能,但对于企业级应用程序来说似乎并不可持续。我发现的所有示例都是这样做的,但是根据我们当前的业务目标,我需要在不 bootstrapping 应用程序模块的情况下执行此操作。
到目前为止,我已经成功注入了 $resource 和 $httpBackend 并且它们可以正常工作。我可以为 $httpBackend 对象分配一个响应行为,我可以将 $resource 注入测试套件中的 restClient 工厂。然而,当我尝试 运行 它时,它们似乎并没有意识到彼此,因此客户端实际上正在发送调用(不受欢迎的#1),让 $httpBackend 对象等待着出色的期望并且没有什么可以刷新的(#2).
请帮忙
你能指出我遗漏的配置错误吗?让这两个部分一起工作而不调用 module()?
测试错误
Error: No pending request to flush !
at Function.$httpBackend.flush (node_modules/angular-mocks/angular-mocks.js:1544:34)
at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:855:22 <- src/app/services/restClient_test.js:24:0)
Error: Unsatisfied requests: GET https://ourlink.nl
at Function.$httpBackend.verifyNoOutstandingExpectation (node_modules/angular-mocks/angular-mocks.js:1577:13)
at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:847:22 <-
restClient XHR 失败导致浏览器错误
XMLHttpRequest cannot load undefinedhttps://oursite.nl?userId=UserZ. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
restClient.js:
function restClient ($resource, baseUrl) {
'use strict';
return {
get: get
};
function get (route, options) {
var self = this;
var payload = $resource(self.baseUrl + route, options).get(options.routeParams);
return payload;
}
}
module.exports = restClient;
restClient_test.js:
describe('restClient', function () {
var client, $httpBackend;
beforeEach(inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
}));
beforeEach(function () {
var $injector = angular.injector(['ng', 'ngResource']);
this.client = require('./restClient.js');
client = this.client($injector.get('$resource'));
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('sends a get request', function () {
$httpBackend.expectGET('https://oursite.nl')
.respond('200', {userId: 'UserZ'}, {token: '1234'});
client.get('https://oursite.nl', {userId: 'UserZ'});
$httpBackend.flush();
});
});
如果不使用 ngMock Jasmine/Mocha 助手,它必须是这样的:
describe('restClient', function () {
var $injector, client, $httpBackend;
beforeEach(function () {
$injector = angular.injector(['ng', 'ngResource', 'ngMock']);
$httpBackend = $injector.get('$httpBackend');
this.client = require('./restClient.js');
client = this.client($injector.get('$resource'));
});
...
});
要求
- Angular 基于 REST 客户端组件
- 必须在私有 API 和服务 之间进行交互
- Karma/Jasmine 测试没有 bootstrap 应用程序模块
目前的困境
我们不想 bootstrap 整个应用程序进行单元测试(使用 module() 或 angular.mock.module())。尽管 Angular 的组件注册是一项有用的功能,但对于企业级应用程序来说似乎并不可持续。我发现的所有示例都是这样做的,但是根据我们当前的业务目标,我需要在不 bootstrapping 应用程序模块的情况下执行此操作。
到目前为止,我已经成功注入了 $resource 和 $httpBackend 并且它们可以正常工作。我可以为 $httpBackend 对象分配一个响应行为,我可以将 $resource 注入测试套件中的 restClient 工厂。然而,当我尝试 运行 它时,它们似乎并没有意识到彼此,因此客户端实际上正在发送调用(不受欢迎的#1),让 $httpBackend 对象等待着出色的期望并且没有什么可以刷新的(#2).
请帮忙
你能指出我遗漏的配置错误吗?让这两个部分一起工作而不调用 module()?
测试错误
Error: No pending request to flush !
at Function.$httpBackend.flush (node_modules/angular-mocks/angular-mocks.js:1544:34)
at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:855:22 <- src/app/services/restClient_test.js:24:0)
Error: Unsatisfied requests: GET https://ourlink.nl
at Function.$httpBackend.verifyNoOutstandingExpectation (node_modules/angular-mocks/angular-mocks.js:1577:13)
at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:847:22 <-
restClient XHR 失败导致浏览器错误
XMLHttpRequest cannot load undefinedhttps://oursite.nl?userId=UserZ. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
restClient.js:
function restClient ($resource, baseUrl) {
'use strict';
return {
get: get
};
function get (route, options) {
var self = this;
var payload = $resource(self.baseUrl + route, options).get(options.routeParams);
return payload;
}
}
module.exports = restClient;
restClient_test.js:
describe('restClient', function () {
var client, $httpBackend;
beforeEach(inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
}));
beforeEach(function () {
var $injector = angular.injector(['ng', 'ngResource']);
this.client = require('./restClient.js');
client = this.client($injector.get('$resource'));
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('sends a get request', function () {
$httpBackend.expectGET('https://oursite.nl')
.respond('200', {userId: 'UserZ'}, {token: '1234'});
client.get('https://oursite.nl', {userId: 'UserZ'});
$httpBackend.flush();
});
});
如果不使用 ngMock Jasmine/Mocha 助手,它必须是这样的:
describe('restClient', function () {
var $injector, client, $httpBackend;
beforeEach(function () {
$injector = angular.injector(['ng', 'ngResource', 'ngMock']);
$httpBackend = $injector.get('$httpBackend');
this.client = require('./restClient.js');
client = this.client($injector.get('$resource'));
});
...
});