Created
January 2, 2014 08:39
-
-
Save abruzzi/8216463 to your computer and use it in GitHub Desktop.
How to mock and test service in angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['controllers/controllers', | |
'services/search-setting-service'], function(controllers) { | |
controllers.controller('SearchSettingController', | |
['$scope', 'SearchSettingService', function($scope, SearchSettingService) { | |
SearchSettingService.setting().then(function(setting) { | |
$scope.setting = setting; | |
$scope.currentVendor = setting.vendors[0]; | |
}); | |
}]); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['angular', | |
'angular-mocks', | |
'services/services', | |
'services/search-setting-service'], | |
function() { | |
describe('Search Setting Service', function() { | |
var settingService; | |
var httpBackend; | |
beforeEach(function() { | |
module('services'); | |
inject(function(SearchSettingService, $httpBackend) { | |
settingService = SearchSettingService; | |
httpBackend = $httpBackend; | |
}); | |
}); | |
afterEach(function() { | |
httpBackend.verifyNoOutstandingExpectation(); | |
httpBackend.verifyNoOutstandingRequest(); | |
}); | |
it("should have setting method", function() { | |
expect(angular.isFunction(settingService.setting)).toBe(true); | |
}); | |
it('should have settings from http request', function() { | |
var result; | |
var expected = { | |
"period": "day", | |
"date": "Sat Dec 21 12:56:53 EST 2013", | |
}; | |
httpBackend.expectGET('/settings.json').respond(expected); | |
var promise = settingService.setting(); | |
promise.then(function(data) { | |
result = data; | |
}); | |
httpBackend.flush(); | |
expect(result).toEqual(expected); | |
}); | |
it("should throw error when network expection", function() { | |
var result, error; | |
httpBackend.expectGET('/settings.json').respond(500); | |
var promise = settingService.setting(); | |
promise.then(function(data) { | |
result = data; | |
}, function(data) { | |
error = data; | |
}); | |
httpBackend.flush(); | |
expect(result).toBeUndefined(); | |
expect(error).toEqual("network error"); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['services/services'], function(services) { | |
services.factory('SearchSettingService', | |
['$http', '$q', function($http, $q) { | |
return { | |
setting: function() { | |
var deferred = $q.defer(); | |
$http.get('/settings.json').success(function(result) { | |
deferred.resolve(result); | |
}).error(function(result) { | |
deferred.reject("network error"); | |
}); | |
return deferred.promise; | |
} | |
}; | |
}]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment