3

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

 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 端点返回值。

但是,当运行 jasmine 规范时,它会失败并显示:

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']

4

1 回答 1

4

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

@httpBackend.whenGET().respond(200, {"someStrings": ["foo", "bar"], otherStrings: ["bar", "goo"]})
于 2013-03-04T20:01:12.240 回答