beforeEach module('Lunch') beforeEach inject(($httpBackend-6ren">
gpt4 book ai didi

javascript - 我正确地 mock httpBackend 吗?

转载 作者:行者123 更新时间:2023-11-28 08:33:18 25 4
gpt4 key购买 nike

单元测试:

describe "Lunch controller", ->
beforeEach module('Lunch')
beforeEach inject(($httpBackend) ->
$httpBackend.expectGET('/lunchers')
.respond(200, ["some content", "some other content"])
)

describe "LunchCtrl", ->
it "should set images to a populated array", inject(($controller, $httpBackend) ->
scope = {}
ctrl = $controller("LunchPoolController",
$scope: scope
)
expect(scope.lunchers.length).toBe 2
)

正在 throw

 at /Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.coffee:13
INFO [watcher]: Changed file "/Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.coffee".
Chrome 32.0.1700 (Mac OS X 10.9.1) Lunch controller LunchCtrl should set images to a populated array FAILED
TypeError: Cannot read property 'length' of undefined
at null.<anonymous> (/Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.js:13:35)
at Object.invoke (http://localhost:3000/assets/application.js:7886:19)
at workFn (http://localhost:3000/assets/application.js:14982:22)
Error: Declaration Location
at window.inject.angular.mock.inject (http://localhost:3000/assets/application.js:14970:27)
at null.<anonymous> (/Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.js:7:57)
at null.<anonymous> (/Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.js:6:10)
at /Users/jd/Dropbox/apps/lookingtolunch/spec/javascripts/controller_specs.js.js:1:1
Chrome 32.0.1700 (Mac OS X 10.9.1): Executed 2 of 2 (1 FAILED) (2.766 secs / 0.201 secs)

最佳答案

假设 LunchPoolController 正在触发 HTTP 请求,您需要使用 $httpBackend.flush() 来确保所有请求均已通过。 (有关更多信息,请参阅 Flushing HTTP requests 部分)。

关于javascript - 我正确地 mock httpBackend 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567502/

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