【问题标题】:ngMock fails to recognize requestngMock 无法识别请求
【发布时间】:2013-02-19 00:55:01
【问题描述】:

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

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

【问题讨论】:

    标签: http angularjs coffeescript jasmine


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 2014-06-08
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多