Skip to content

Instantly share code, notes, and snippets.

@taktran
Last active August 29, 2015 14:05
Show Gist options
  • Save taktran/56e2efa6945ce28855e4 to your computer and use it in GitHub Desktop.
Save taktran/56e2efa6945ce28855e4 to your computer and use it in GitHub Desktop.
An example of how to do dependency injection in angularjs specs
describe('currentEnvService', function() {
var currentEnvService;
var setupMocks;
var location = {};
var hostNamesMock = {};
beforeEach(angular.mock.module('app'));
beforeEach(function() {
// Dependency inject values with global objects.
// Global objects can then be changed in the
// tests when need be.
angular.mock.module(function($provide) {
$provide.value('$window', {
location: location
});
$provide.constant('ENV_HOSTNAMES', hostNamesMock);
});
setupMocks = function(url, hostNamesMockVal) {
//se Update dependency injected values
location = {
hostname: url
};
_.extend(hostNamesMock, hostNamesMockVal);
// NOTE: Need to reinitialise each time
// so that the dependency injected values
// are reinitialised
inject(function(_currentEnvService_) {
currentEnvService = _currentEnvService_;
});
};
});
it("returns current environment from window location", function() {
setupMocks("testingUrl.com", {
test: "testingUrl.com"
});
expect(currentEnvService).toEqual("test");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment