lundi 26 janvier 2015

AngularJS: Mock Controller Service

I am trying to mock a service for a controller but struggling to get it to work.


Using semi-psudeo code I have a service:



.factory('SomeService', ['$http'function($http) {

var someService = {};

someService.getData=function(val1, val2){
...
return someData;
}

return someService;
}


And a controller like so:



.controller('someController', ['SomeService', function(SomeService){
var self = this;
var data = [];

self.getSomeData = = function(){
var response = SomeService.getSomething.then(function(response){
self.data = response;
});
};
}]);


I am trying to write a test that uses a $provide to mock the service. Something like this:



var mockService

beforeEach(module(function($provide){
mockService.getSomething = function(){

return [ ....];
};

$provide.value('SomeService', mockService);

}));


After injecting this mock and running the test I get:


Typeerror: cannot set property getSomething of undefined


Is there anything obviously wrong with this code (or right)?


Aucun commentaire:

Enregistrer un commentaire