简体   繁体   中英

Test that a request was sent in a Rails controller

我有一个控制器在某个操作上发布了HTTP请求,如何测试/声明该请求实际上是发送的?

I would just test the response or the higher level action (like what does this thing do?). But if you want to test the interactions (maybe routes check etc?) then mock it up. Below are some ideas.

# 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 type test:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM