mercredi 24 décembre 2014

Mock with jasmine

I want to isolate my angular unit specs.


My code base consists of a factory and a controller:



.factory('ServiceData', ->
{ message: 'im data from a factory service' }
)
.controller('Controller', ['$scope','ServiceData',($scope, ServiceData)->
$scope.serviceData = ServiceData


And here is a spec for the controller:



it 'should get data from a service', ->
spyOn(ServiceData, 'message').andReturn('im from a mock') # this doesn't work
expect($scope.serviceData).toEqual 'im from a mock'


The spec doesn't work, because I get the following error:



ReferenceError: ServiceData is not defined in ...controller_spec.js


I want to isolate my controller spec from my factory. How do I achieve this?


Also, this is a trivial example, but in future I will have very complex factories that get data from the server and all sorts. So I want to know how to stub them out.


I also might want to test how my controller behaves based on different things that are returned by the mocked out function.


Aucun commentaire:

Enregistrer un commentaire