更新 angular-路由 1.5.0 到 1.5.1 时出现 Karma 错误
Karma Error when update angular-route 1.5.0 to 1.5.1
当我更新 angular-route 1.5.0 到 1.5.1 时,出现错误:
Angular 单元测试:错误:意外请求:GET
当我启动 karma 时,我收到一条错误消息:
1) Call getAll method
[App Category]
Error: Unexpected request: GET http://myapp.com/app-category?is_active=true No more request expected in
/node_modules/angular-mocks/angular-mocks.js
app_category.model.test.js
describe('[App Category]', function () {
beforeEach(module('myApp'));
var $httpBackend, HttpService, AppCategory;
beforeEach(inject(function (_$httpBackend_, _HttpService_, _AppCategory_) {
$httpBackend = _$httpBackend_;
HttpService = _HttpService_;
AppCategory = _AppCategory_;
}));
it('Call getAll method', function () {
var app_category = new AppCategory();
HttpService.mock('GET', 'app-category?is_active=true', 200, [{ code: 'TRAVEL', name: 'Travel' }]);
app_category.getAll({ is_active: true }).then(function (request) {
expect(request.data[0].code).toBe('TRAVEL');
expect(request.data[0].name).toBe('Travel');
});
$httpBackend.flush();
});
});
angular-mockHTTP.js
(function (angular) {
'use strict';
angular.module('ngMockHTTP', []).service('HttpService', function ($httpBackend, config) {
this.endpoint = config.api.endpoint;
this.mock = function (method, url, status, response) {
switch (method) {
case 'GET':
$httpBackend
.expectGET(this.endpoint + url)
.respond(status, response);
break;
case 'POST':
$httpBackend
.expectPOST(this.endpoint + url)
.respond(status, response);
break;
case 'PUT':
$httpBackend
.expectPUT(this.endpoint + url)
.respond(status, response);
break;
case 'PATCH':
$httpBackend
.expectPATCH(this.endpoint + url)
.respond(status, response);
break;
case 'DELETE':
$httpBackend
.expectDELETE(this.endpoint + url)
.respond(status, response);
break;
}
};
});
})(angular);
我试过替换 :
case 'GET':
$httpBackend
.expectGET(this.endpoint + url)
.respond(status, response);
break;
通过这个 :
case 'GET':
$httpBackend
.whenGET(this.endpoint + url, {
}).respond(function(){ return [200, response]});
break;
但是我有同样的错误
我使用 jasmine 2.4.1,angularjs 1.5.3,karma 0.13.0
此错误已为 angular 团队所知。
此错误的解决方法是为 $templateCache 定义一个虚拟模板。
更多信息:link
我更新了 angularjs 1.5.5,它有效!
来自 CHANGELOG.md 1.5.5(2016 年 4 月 18 日发布):
"ngRoute: allow ngView to be included in an asynchronously loaded
template Eagerly loading $route, could break tests, because it might
request the root or default route template (something $httpBackend
would know nothing about).
It will be re-applied for v1.6.x, with a breaking change notice and
possibly a way to disable the feature in tests."
当我更新 angular-route 1.5.0 到 1.5.1 时,出现错误:
Angular 单元测试:错误:意外请求:GET
当我启动 karma 时,我收到一条错误消息:
1) Call getAll method [App Category] Error: Unexpected request: GET http://myapp.com/app-category?is_active=true No more request expected in /node_modules/angular-mocks/angular-mocks.js
app_category.model.test.js
describe('[App Category]', function () {
beforeEach(module('myApp'));
var $httpBackend, HttpService, AppCategory;
beforeEach(inject(function (_$httpBackend_, _HttpService_, _AppCategory_) {
$httpBackend = _$httpBackend_;
HttpService = _HttpService_;
AppCategory = _AppCategory_;
}));
it('Call getAll method', function () {
var app_category = new AppCategory();
HttpService.mock('GET', 'app-category?is_active=true', 200, [{ code: 'TRAVEL', name: 'Travel' }]);
app_category.getAll({ is_active: true }).then(function (request) {
expect(request.data[0].code).toBe('TRAVEL');
expect(request.data[0].name).toBe('Travel');
});
$httpBackend.flush();
});
});
angular-mockHTTP.js
(function (angular) {
'use strict';
angular.module('ngMockHTTP', []).service('HttpService', function ($httpBackend, config) {
this.endpoint = config.api.endpoint;
this.mock = function (method, url, status, response) {
switch (method) {
case 'GET':
$httpBackend
.expectGET(this.endpoint + url)
.respond(status, response);
break;
case 'POST':
$httpBackend
.expectPOST(this.endpoint + url)
.respond(status, response);
break;
case 'PUT':
$httpBackend
.expectPUT(this.endpoint + url)
.respond(status, response);
break;
case 'PATCH':
$httpBackend
.expectPATCH(this.endpoint + url)
.respond(status, response);
break;
case 'DELETE':
$httpBackend
.expectDELETE(this.endpoint + url)
.respond(status, response);
break;
}
};
});
})(angular);
我试过替换 :
case 'GET':
$httpBackend
.expectGET(this.endpoint + url)
.respond(status, response);
break;
通过这个 :
case 'GET':
$httpBackend
.whenGET(this.endpoint + url, {
}).respond(function(){ return [200, response]});
break;
但是我有同样的错误
我使用 jasmine 2.4.1,angularjs 1.5.3,karma 0.13.0
此错误已为 angular 团队所知。
此错误的解决方法是为 $templateCache 定义一个虚拟模板。
更多信息:link
我更新了 angularjs 1.5.5,它有效!
来自 CHANGELOG.md 1.5.5(2016 年 4 月 18 日发布):
"ngRoute: allow ngView to be included in an asynchronously loaded template Eagerly loading $route, could break tests, because it might request the root or default route template (something $httpBackend would know nothing about).
It will be re-applied for v1.6.x, with a breaking change notice and possibly a way to disable the feature in tests."