[英]Angular/Jasmine: adding .error to callback seems to break unit tests
我遇到了僅在定義了HTTP回調的.error部分時發生的錯誤。 這是我正在測試的控制器的相關部分:
describe('SimpleControllerTests', function () {
var scope;
var expectedResponse = [{count:'101', time:1416960000000}];
var $httpBackend, $controller, $timeout, uuid4;
beforeEach(module('dashboardApp'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$httpBackend_, _$timeout_,_uuid4_){
$controller = _$controller_;
$httpBackend = _$httpBackend_;
$timeout =_$timeout_;
scope = _$rootScope_;
uuid4 = _uuid4_;
spyOn(uuid4,'generate').and.returnValue("1");
}));
// makes sure all expected requests are made by the time the test ends
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('should load data successfully', function() {
beforeEach(function() {
$httpBackend.expectPOST('http://localhost/folder/index', {"jsonrpc":"2.0","method":"getData","id":"1","params":{"period":"week"}}).response(expectedResponse);
$controller('HomeCtrl as vm', { $scope: scope });
$httpBackend.flush();
});
it('using dirtyTestGraph()', function() {
scope.vm.dirtyTestGraph();
$timeout.flush();
scope.$digest();
expect(scope.vm.chartData).toEqual(expectedResponse);
});
});
describe('should fail to load data', function() {
beforeEach(function() {
$httpBackend.expectPOST('http://localhost/folder/index',
{"jsonrpc":"2.0","method":"getData","id":"1","params":{"period":"week"}}).response(500);
$controller('HomeCtrl as vm', { $scope: scope });
$httpBackend.flush();
});
it('using dirtyTestGraph()', function() {
scope.vm.dirtyTestGraph();
$timeout.flush();
scope.$digest();
expect(scope.vm.chartData).toEqual('');
});
});
});
httpBackend.verifyNoOutstandingExpecation失敗// //-不滿意的請求:POST http:// localhost / folder / index
就像beforeEach中的$ httpBackend.expectPost一樣:'undefined'不是一個函數(在'...“:” week“}}附近).response(expectedResponse);
似乎我的作用域沒有正確創建,我得到:'undefined'不是評估'scope.vm.graphLoading'的對象
我使用“ controller as”語法聲明控制器,並且在其他測試中也使用“ ctrl as vm”,並且我可以很好地引用它,不確定發生了什么。
我忘記了控制器方法:
vm.dirtyTestGraph = function() {
$timeout(function(){
ChartService.get( { period: 'week'} )
.success(function(res){
vm.graphLoading = false;
vm.chartData = res.data;
}).error(function(err){
console.log(err);
});
}, 2000);
};
如果你看這個:
ChartService.get( {...} ).success(function(res){...}).error(function(err){...});
您會看到,您將錯誤稱為“成功”,而不是“ get”,因為您已鏈接函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.