使用$ httpBackend进行业力测试 - 没有待处理的待处理请求

时间:2015-12-08 14:57:26

标签: angularjs coffeescript jasmine karma-runner httpbackend

前言:我有两个名为injectionscontrollerInjections的方法,它们简化了注入测试的过程。它将注射放在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()

我一直在"没有待处理的冲洗请求!"调用flush()方法时。但是,当我删除whenGET的{​​{1}}时,我收到了一个"意外请求",这意味着该请求肯定被调用。因此,/api/users/... 拥有要清除的内容。

当我取消注释flush()电话时,我似乎在测试结束时没有出色的期望。

expectGET用于访问@UserProfileController.loadUserInfo()的API并在完成时进行回调。回调永远不会运行,虽然请求似乎是发送的(从我在调试器中看到的)但没有收到响应。

有什么想法吗?

0 个答案:

没有答案
相关问题