I am trying to do an unit test a service in my case
In my test controller
myService.getItem('/api/toy/' + scope.id).success(
function(toy) {
$scope.toy = toys.details;
}
);
MyService
angular.module('toyApp').service('myService', ['$http',
function($http) {
var service = {};
return {
getItem: function(url) {
return $http.get(url);
},
};
}
]);
Test file.
describe('toy ctrl', function () {
var $httpBackend, ctrl, myService;
beforeEach(module('toyApp'));
beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, __myService_) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
myService = _myService_;
ctrl = _$controller_('toyCtrl', {
$scope: scope
});
}));
describe('call my service', function() {
it('should make request when app loads', function() {
$httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456 });
myService.getItem('/api/toy/123').then(function(toy){
expect(scope.toy.detail).toBe(456);
})
$httpBackend.flush();
})
})
I am getting
Error: Unexpected request: GET /api/toy/123
No more request expected
If I take out $httpBackend.flush(), the error is gone but it won't cover the
function(toy) {
$scope.toy = toys.details;
}
part. I want to cover the function call and not sure how to do this. Can anyone help me about it? Thanks a lot
myService.getItem('/api/toy/123')is irrelevant actually.