简体   繁体   English

如何在 Rails 的控制器测试中测试第二个重定向

[英]How to test a second redirect in Rails' controller test

I am using Wicked which redirects to a self-defined link after finishing the wizard.我正在使用Wicked ,它会在完成向导后重定向到自定义链接。 This happens with a second redirect.这发生在第二次重定向时。

So, a PUT update triggers a 302 to /orders/1/finish_wicked, which then redirects to /orders/1.因此, PUT update会触发302到 /orders/1/finish_wicked,然后重定向到 /orders/1。 This works as expected, but is hard to test in my controller tests.这按预期工作,但很难在我的控制器测试中进行测试。

it "must redirect to the order" do
  put :update
  assert_redirected_to "/orders/1/finish_wicked" #=> This passes, but I am not interested in this
  assert_redirected_to order #=> This fails, yet, in the end, the user is being redirected here.
end

How can I test a second redirect in my functional -controller- tests?如何在我的功能控制器测试中测试第二个重定向?

Right now, I have it split into two tests:现在,我将它分为两​​个测试:

  describe "finalize" do
    it "should redirect to the wicked_finish page" do
      put :update, id: :finalize, order_id: order.id, order: { accepted: true }
      assert_redirected_to controller: "build", action: :update, id: :wicked_finish, order_id: order.id
    end

    describe "wicked_finish" do
      it "should redirect to the order page" do
        get :show, id: :wicked_finish, order_id: order.id
        assert_redirected_to order
      end
    end
  end

One tests that it is redirected to the wicked-provided finish-path, the other to define that if a user lands there, she is redirected to the order.一个测试它被重定向到邪恶提供的完成路径,另一个定义如果用户登陆那里,她被重定向到订单。 This is overly verbose;这过于冗长; is it not possible to follow redirects in a controller-test?是否无法在控制器测试中跟踪重定向? Or is this a bad idea and should the tests be kept split-up, like I have?或者这是一个坏主意,应该像我一样将测试分开吗?

Uhmmmmm not sure if this is a proper answer but i have seen some bad code that do something like this.嗯嗯不确定这是否是一个正确的答案,但我见过一些糟糕的代码做这样的事情。 Make the call, when you have redirected it to a page, the response contains redirecting to bla bla bla.拨打电话,当您将其重定向到页面时,响应包含重定向到 bla bla bla。 In my case the redirection page depended on the params passed hence i knew the link before hand and i asserted the presence of the url/link there.在我的情况下,重定向页面取决于传递的参数,因此我事先知道链接,并且我断言那里存在 url/链接。

It is possible by following the first redirection, use follow_redirect!可以按照第一个重定向,使用follow_redirect! :

test 'assert second redirect' do
  put :update
  assert_redirected_to "/orders/1/finish_wicked"
  follow_redirect!
  assert_redirected_to order 
end

Apidock follow_redirect! Apidock follow_redirect!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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