I am just starting getting my head around unit testing and angular 1.2. I am using the $httpBackend service for testing a $httpcall in a service. This service is called inside my controller:
var app = angular.module('myApp', []);
app.controller('MainCtrl', function ( someService) {
var vm = this;
vm.hasError = false;
//debugger;
someService.someAsyncCall()
.then(function (data) {
vm.hasError = false;
})
.catch(function (data) {
vm.hasError = true;
});
});
app.factory('someService', function ($http) {
return {
someAsyncCall: function () {
return $http.get("/data")
.success(function (data) {
//return true
})
.error(function () {
console.log('error');
//return false
});
}
};
});
I am trying to write a test for when the endpoint is wrong:
beforeEach(function () {
someServiceMock = jasmine.createSpyObj('someService', ['someAsyncCall']);
module('myApp');
inject(function ($rootScope, $controller, $q, _$timeout_,$httpBackend) {
$scope = $rootScope.$new();
someServiceMock.someAsyncCall.andReturn($q.when('weee'));
$timeout = _$timeout_;
controllerService = $controller;
httpMock = $httpBackend;
});
});
it("should set hasError=true with error request", function () {
httpMock.expectGET("/datajfds").respond(false);
ctrl = controllerService('MainCtrl', {someService: someServiceMock});
expect(ctrl.hasError).toBe(true);
});
This is the error:
MainCtrl testing should set hasError=true with error request.
Expected false to be true.
Error: Expected false to be true.
at new jasmine.ExpectationResult (http://ift.tt/29mjj3w)
at .toBe (http://ift.tt/29iU2DO)
at .<anonymous> (http://ift.tt/29miIyQ)
Basically I would like to get coverage/write a test for when the someService.someAsyncCall() will catch an error. How can I write this test so that ctrl.hasError =true?
plunkr:http://ift.tt/29vYEex
Aucun commentaire:
Enregistrer un commentaire