Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This happens with a second redirect.

So, a PUT update triggers a 302 to /orders/1/finish_wicked, which then redirects to /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?

like image 225
berkes Avatar asked Mar 21 '14 10:03

berkes


Video Answer


2 Answers

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. 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.

like image 104
AshwinKumarS Avatar answered Nov 07 '22 23:11

AshwinKumarS


It is possible by following the first redirection, use 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!

like image 21
Luis Adrián Chávez Fregoso Avatar answered Nov 07 '22 22:11

Luis Adrián Chávez Fregoso