测试在Rails控制器中发送请求

时间:2012-03-27 18:27:34

标签: ruby-on-rails testing

我有一个控制器在特定操作上发布HTTP请求,如何测试/断言请求实际发送?

1 个答案:

答案 0 :(得分:0)

我只是测试响应或更高级别的动作(就像这件事做的那样?)。但是如果你想测试交互(也许路由检查等?)然后模拟它。以下是一些想法。

# 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)