Skip to content

Instantly share code, notes, and snippets.

@ryanirilli
Last active August 29, 2015 14:23
Show Gist options
  • Save ryanirilli/baf90beaad7fd37d7c8d to your computer and use it in GitHub Desktop.
Save ryanirilli/baf90beaad7fd37d7c8d to your computer and use it in GitHub Desktop.
var specHelper = (function(){
var defaultUser = {...}
var mockedServices;
var files = fs.readdirSync('./service_mocks');
//load all the mocked services
function loadMockServices(){
mockedServices = {};
_.forEach(files, function(file) {
var mock = require(path.join(__dirname, '/service_mocks', file);
mockedServices[mock.serviceName] = mock.service;
});
module(function ($provide) {
_.forEach(mockedServices, function(val, key){
$provide.value(key, val);
});
}
}
function getMockedService(name) {
return mockedServices[name];
}
function restoreMockedService(name) {
var mock = require('path/to/service/mock' + name + '.js');
$provide.value(mock.serviceName, mock.service);
}
function getControllerInstance(name) {
var instance;
inject(function ($rootScope, $controller) {
instance = $controller(name, {
$scope: $rootScope.$new();
});
});
return instance;
}
function mockMethodThatReturnsAPromise(instance, method, willResolve, data) {
instance[method] = function(){
return {
then: function(callback){
if(willResolve) { callback(data) }
return {
catch: function(reject){
if(!willResolve) { reject(data) }
}
}
},
}
}
}
return {
getMockedService: getMockedService,
getControllerInstance: getControllerInstance,
mockMethodThatReturnsAPromise: mockMethodThatReturnsAPromise,
loadMockServices: loadMockServices //should be used in afterEach to restore all mocks
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment