gpt4 book ai didi

http - ngMock 无法识别请求

转载 作者:可可西里 更新时间:2023-11-01 16:10:15 25 4
gpt4 key购买 nike

我有以下关于 Controller 的规范。我有以下 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()

环绕以下 Controller :

 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 规范运行时,它失败了:

错误:意外请求:GET/api/foos/17 在 http://localhost:3000/assets/helpers/angular-mocks.js?body=1(第 889 行)中预计不再有请求

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

scope.initialize = (artifactId, artifactType)-> 范围.artifactId = artifactId scope.artifactType = 工件类型 scope.currentVersionExists = false 作用域.attachmentFetcher(scope.artifactId)

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

最佳答案

在发出请求之前,您应该在响应中添加 stub 的这一行:

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

关于http - ngMock 无法识别请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com