angularjs - AngularJS:带有 $http 调用的测试方法引发错误
问题描述
我在 AngularJS 中有这样的控制器:
(angular => {
'use strict';
const component = {
bindings: {
fileLoaded: '&'
},
templateUrl:
'/some-component/some-component.template.html',
controller
};
function controller(SomeService) {
const ctrl = this;
ctrl.data = null;
ctrl.isLoading = false;
ctrl.saveIt = (data) => {
ctrl.isUpdating = true;
SomeService.setData(data).then(
() => {
ctrl.isLoading = false;
ctrl.data = data;
},
() => {
ctrl.isLoading = false;
});
};
}
controller.$inject = [ 'SomeService' ];
angular.module('myApp').component('someComponent', component);
})(window.angular);
我尝试编写这样的测试:
'use strict';
describe('someComponent Component', () => {
let $componentController;
let $rootScope = null;
let $scope = null;
let $httpBackend = null;
let SomeService = null;
beforeEach(module('myApp'));
beforeEach(inject(function(_$componentController_, _$rootScope_, _$httpBackend_, _SomeService_) {
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
SomeService = _SomeService_;
$scope = $rootScope.$new();
}));
describe('method: saveIt', () => {
it('call saveIt (POST)', () => {
const ctrl = $componentController('someComponent', null, {});
const data = { someData: 'someDataSomeData' };
ctrl.saveIt(data);
$httpBackend
.expectPOST('/someURL')
.respond(200, {});
SomeService.setData(data)
.then(() => {
ctrl.isLoading = false;
ctrl.data = data;
})
.catch(() => {
ctrl.isLoading = false;
});
$httpBackend.flush();
expect(ctrl.data).not.toBe(null);
expect(ctrl.isLoading).toBe(false);
});
});
});
但它只是在没有任何解释的情况下引发错误:什么和哪里错了
如何调试和解决它?我做错了什么?
解决方案
推荐阅读
- flutter - 在颤动中滚动列表视图时剪辑填充
- flutter - 每当我的流获得新值时,如何显示对话框?
- kubernetes - 为什么我得到一个空文件?
- python - 无法在 Python 网页抓取中获取文章的链接
- postman - 邮递员模拟服务器呼叫日志为空
- testing - 辅助功能测试(使用 Microsoft 讲述人作为屏幕阅读器)?有人在这里提供一些实用指南吗?
- python - 对为什么生成器有用感到困惑
- firebase - Firebase 中的现收现付定价和免费定价的混淆
- bash - 在bash中将周数转换为日期范围
- angular - Angular 6 目标 es5 IE 语法错误(箭头函数)