2

Preface: I have 2 methods called injections and controllerInjections which simply simplify injecting into the test. It puts the injections inside this.

My test looks like this:

describe "user profile controller", ->
  beforeEach module(APP_NAME)
  beforeEach module('ngMockE2E')
  afterEach =>
    @$httpBackend.verifyNoOutstandingExpectation()
    @$httpBackend.verifyNoOutstandingRequest()

  $stateParams =
    slug: 'some-person'

  user =
    id: 1566
    slug: 'some-person'
    full_name: 'Some person'

  # this method takes a callback when injections are done
  injections.call @, '$httpBackend', => 
    @$httpBackend
      .whenGET(/\/api\/users\/([a-z0-9\-\_]+)\/?/)
      .respond [200, user]

    # @$httpBackend.expectGET("/api/users/#{$stateParams.slug}")

    @$httpBackend
      .whenGET(/^\/views\//)
      .respond 200, ''

  # this method takes extra parameters to push into the controller
  controllerInjections.call @, 'UserProfileController', {$stateParams}

  it "should load user info when requested", =>
    resp = null

    @scope.$apply =>
      resp = @UserProfileController.loadUserInfo()

    # console.debug 'user', @UserProfileController.user

    # debugger
    console.debug 'resp', resp
    @$httpBackend.flush()

I keep getting "No pending request to flush !" when the flush() method is called. But when I remove the whenGET for /api/users/..., I get an "unexpected request", meaning that the request is definitely being called. So flush() should have what to flush out.

When I uncomment the expectGET call I don't seem to have outstanding expectations at the end of the test.

@UserProfileController.loadUserInfo() is used to access an API with $http and do a callback on finish. The callback never runs, though the request seems to be sent (from what I see in the debugger) but not receiving responses.

Any ideas?

casraf
  • 21,085
  • 9
  • 56
  • 91

0 Answers0