我遇到了一个错误,只有在我定义了我的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
&p>在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);
};
答案 0 :(得分:0)
如果你看一下:
ChartService.get( {...} ).success(function(res){...}).error(function(err){...});
你会看到,你在“成功”上调用错误,而不是在“获取”上调用错误,因为你链接了函数