gpt4 book ai didi

ruby-on-rails - 测试请求是否已在 Rails Controller 中发送

转载 作者:行者123 更新时间:2023-11-28 20:57:25 24 4
gpt4 key购买 nike

我有一个针对特定操作发布 HTTP 请求的 Controller ,我该如何测试/断言该请求实际上已被发送?

最佳答案

我只会测试响应或更高级别的操作(比如这个东西有什么作用?)。但是如果你想测试交互(也许路由检查等?)然后模拟它。以下是一些想法。

# test where the request goes
request = ActionController::TestRequest.new
request.query_parameters[:foo] = "bar"
request.path = "/"
ActionController::Routing::Routes.recognize(request)
assert_equal({"controller"=>"your_controller","action"=>"index"}, request.path_parameters)
# other tests here if you want

Rspec 类型测试:

# test that the controller will receive it
controller = YourController.new
controller.should respond_to(:create)

关于ruby-on-rails - 测试请求是否已在 Rails Controller 中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9895376/

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