ngMock无法识别请求

时间:2013-03-04 19:55:00

标签: http angularjs coffeescript jasmine

我在控制器周围有以下规格。我有以下茉莉花规格:

 describe 'MyApp.Controller.SomeController', ->
   beforeEach module('mymodule')
   beforeEach inject ($rootScope , $httpBackend, $controller, SomeService) ->
      @scope = $rootScope.$new()
      @httpBackend = $httpBackend
      someService = SomeService
      @someResource = someService.someResource
      $controller 'MyApp.Controller.SomeController', $scope: @scope

   describe "#fetchMethod", ->
     describe "given an object", ->
       beforeEach ->
         @id = 17
         @scope.fetchMethod(@id)
       it "sets due to true", ->
         @httpBackend.whenGET().respond(200, {"someStrings": ["foo", "bar"], otherStrings: ["bar", "goo"]})
         expect(@scope.someStrings).toBeDefined()
         expect(@scope.otherStrings).toBeDefined()

包裹以下控制器:

 MyApp.Controller.SomeController = (scope, someService) ->

   scope.fetchMethod = (ID)->
      someService.someResource.fetch
      Id: artID
      ,(response) ->
        scope.someStrings = response['someStrings']
        scope.otherStrings = response['otherStrings']
        scope.someStringsExist = true if scope.someStrings
 MyApp.Controller.SomeController.$inject = ['$scope', 'SomeService']

其中SomeService的定义如下:

 MyApp.Service.SomeService = (resource) ->
   @someResource = resource '/api/foos/:ID', {},
   fetch:
     method: 'GET'

   @

 MyApp.myModule.service 'SomeService', ['$resource', MyApp.Service.SomeService]

此设置似乎在站点上运行,正确执行请求并从(rails)api端点返回值。

然而,当茉莉花规格运行时,它失败了:

Error: Unexpected request: GET /api/foos/17 No more request expected in http://localhost:3000/assets/helpers/angular-mocks.js?body=1 (line 889)

我错过了什么?为什么httpBackend无法识别GET请求?

scope.initialize =(artifactId,artifactType) - >     scope.artifactId = artifactId     scope.artifactType = artifactType     scope.currentVersionExists = false     scope.attachmentFetcher(scope.artifactId)

MyApp.Controller.SomeController。$ inject = ['$ scope','SomeService']

1 个答案:

答案 0 :(得分:4)

您应该在发出请求之前存储响应的这一行:

@httpBackend.whenGET().respond(200, {"someStrings": ["foo", "bar"], otherStrings: ["bar", "goo"]})
相关问题