I am trying to test an asynchronous method in angular. Here is code from my controller:
if $routeParams.mode == "new"
# some stuff
mySvc.GetMyStuff $routeParams.id
.then (stuff) ->
if stuff? and stuff.length > 1
# do more stuff
else
$scope.stuff = 'foo'
And my test:
describe "test stuff",
$q = {}
$scope = {}
controller = {}
mySvc = {}
beforeEach inject (_$controller_, _$rootScope_, _$q_, _$routeParams_, _mySvc_)
# init stuff
# create a promise to be resolved
d = $q.defer()
d.resolve 'foo' # resolving with an empty array to test 'no results' condition
spyOn(mySvc,'GetMyStuff').and.returnValue d.promise
# later
it "should set stuff to an 'foo'", () ->
mySvc.GetMyStuff "id"
$scope.$apply()
expect($scope.stuff).toBe 'foo' # Expected undefined to be 'foo'
I've also tried calling $scope.$apply() from the beforeEach block, and tried the expectation with simple strings instead of an empty array. I am expecting the data returned by the asynchronous operation to equal an empty array, which should trigger the condition that sets $stuff to 'foo'. It doesn't seem that .then is being called with my resolved value.
Aucun commentaire:
Enregistrer un commentaire