I encountered an error that occurs only when I have the .error part of my http callback defined. Here is the relevant part of the controller I'm testing:
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 fails on // <- Unsatisfied requests: POST http://localhost/folder/index
as does $httpBackend.expectPost in the beforeEach with: 'undefined' is not a function (near '...":"week"}}).response(expectedResponse);
it also appears that my scope isn't getting created properly, I get: 'undefined' is not an object evaluating 'scope.vm.graphLoading'
I use the 'controller as' syntax for declaring controllers, and i've used 'ctrl as vm' in other tests and i'm able to reference it fine, not sure what's going on there.
I forgot the controller method:
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);
};
if you look at this:
ChartService.get( {...} ).success(function(res){...}).error(function(err){...});
you will see, that you call error on "success", and not on "get", because you chained the functions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.