In my service function, here's how it looks:
asyncGetStuff: (id) ->
stuff = []
@asyncGetItem id
.then (item) ->
#parse some data
stuff.push data
return stuff
Now I am trying to verify the contents of 'stuff':
describe "Stuff Service", () ->
beforeEach module 'MyServices'
$scope = undefined
$q = undefined
stuffSvc = undefined
itemSvc = undefined
mockItem = {
item: 'blah blah blah'
}
mockStuff: {
stuff: 'foo'
}
beforeEach inject ($rootScope, _$q_, _stuffSvc_, _itemSvc_) ->
$scope = $rootScope.$new()
$q = _$q_
stuffSvc = _stuffSvc_
itemSvc = _itemSvc_
describe "async Stuff Method", () ->
beforeEach () ->
deferred = $q.defer()
deferred.resolve mockItem
spyOn(itemSvc, 'asyncGetItem').and.returnValue deferred.promise
$scope.$apply()
it "should call asyncGetItem", () ->
stuffSvc.asyncGetStuff()
expect(itemSvc.asyncGetItem).toHaveBeenCalled() ## this asserts properly
it "should return a promise", () ->
promise = stuffSvc.asyncGetStuff()
expect(promise.then).toBeDefined() ## this asserts properly
# how to access stuff returned by asyncGetStuff?
promise = stuffSvc.asyncGetStuff()
## I'm sure this is wrong
promise.then (stuff) -> expect(stuff).toEqual(mockStuff) ## this is ignored
I want to validate that the result of asyncGetStuff has been processed correctly, but I'm having trouble accessing that value. I am confident that the nested async method is resolving the promise correctly, but I need help beyond that.