gpt4 book ai didi

ruby-on-rails - 使用 expectPOST 测试 AngularJS Controller

转载 作者:行者123 更新时间:2023-12-04 06:05:22 25 4
gpt4 key购买 nike

我有一个 AngularJS/Rails 应用程序,我想测试我的 AngularJS Controller 是否在创建新记录时发布到后端服务器。 (我正在使用 jasmine 进行测试)

这是我尝试过的测试

  describe "create", ->
beforeEach(inject ( ($controller, $rootScope, $location, $state, $httpBackend) ->
@redirect = spyOn($location, 'path')
@httpBackend.whenGET('/assets/layouts/default.html.erb').respond(200)
@httpBackend.whenGET('/assets/letters/index.html.erb').respond(200)
@httpBackend.whenPOST('/api/letters').respond(200)

$controller("LettersController", { $scope: @scope, $location: @location })
))

it "sends a post to the backend", ->
@httpBackend.expectPOST('/api/letters', {"letter":{},"_utf8":"☃"}).respond(200)
@scope.create()

这是我正在测试的代码:

  $scope.create = ->
Letter.save(
{}
,
letter:
subject: $scope.letter.subject
body: $scope.letter.body

# success
, (response) ->
$location.path "/letters"
# failure
, (response) ->
)

有问题的代码工作正常并且测试通过。问题是,如果我将我的 Letter.save 代码注释掉(通过 AngularJS 资源发布帖子),那么我的测试仍然会通过。

如何让我的测试正常工作?

我的完整测试应用程序在这里:https://github.com/map7/angularjs_rails_example2

最佳答案

您需要验证在测试结束时没有未完成的请求:

$httpBackend.verifyNoOutstandingRequest();

关于ruby-on-rails - 使用 expectPOST 测试 AngularJS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18625783/

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