How can I keep track of Angular's promise chain using Jasmine

Using AngularJS, CoffeeScript, and Jasmine (edited in WebStorm), I would like to unit test the promises chain.

Suppose I have the following service example:

Angular Service

class ExampleService
    stepData: []
    constructor: (@$http) ->

    attachScopeMethod: (@scope) ->
        @scope.callSteps = => @step1().then -> @step2()

    step1: ->
        @$http.get('app/step/1').then (results) =>
            @stepData[0] = results.data
            results

    step2: ->
        @$http.get('app/step/2').then (results) =>
            @stepData[2] = results.data
            results

This service allows me to bind a method callSteps()to a scope. This method, when called, makes a series of asynchronous $ http calls to a third-party API.

To verify that every step is at least called, I wrote the following Jasmine specification.

Jasmine spectrum

ddescribe 'ExampleService', ->

    beforeEach ->
        module 'myApp'

    beforeEach inject ($rootScope, $injector) ->
        @scope = $rootScope.$new()
        @exampleService = $injector.get 'exampleService'
        @q = $injector.get '$q'

    describe 'process example steps', ->
        beforeEach  -> 
            @exampleService.attachScopeMethod(@scope)

        it "should attach the scope method", ->
            expect(@scope.callSteps).toBeDefined()

        describe 'when called should invoke the promise chain', ->

        it "should call step1 and step2", ->
            defer = @q.defer()
            @exampleService.step1 = jasmine.createSpy('step1').andReturn(defer.promise)

            @exampleService.step2 = jasmine.createSpy('step2')

            @scope.callSteps()
            defer.resolve()

            expect(@exampleService.step1).toHaveBeenCalled()
            expect(@exampleService.step2).toHaveBeenCalled()

The results of this test are as follows:

  • expect (@ exampleService.step1) .toHaveBeenCalled () - PASS
  • expect (@ exampleService.step2) .toHaveBeenCalled () - FAIL

Can you tell me how I can get step2()to successfully complete the test?

thanks

EDIT

@ . , scope.$apply scope.$digest, .

, .

describe 'when called should invoke the promise chain', ->
    it "should call step1 and step2", ->
        defer = @q.defer()
        defer.resolve()

        @exampleService.step1 = jasmine.createSpy('step1').andReturn(defer.promise)
        @exampleService.step2 = jasmine.createSpy('step2')

        @scope.callSteps()
        @scope.$apply()

        expect(@exampleService.step1).toHaveBeenCalled()
        expect(@exampleService.step2).toHaveBeenCalled()
+4
1

$rootScope. $apply()

defer.resolve(). , , , , .

$q.defer(), andReturn()

defer.resolve(true), defer.reject(false), insinde, true false

+3

All Articles