Rails assert_redirect_to

routes.rb

map.resources :places do |places| places.resources :reviews end 

place model

 class Place < ActiveRecord::Base has_many :reviews end 

review model

 class Review < ActiveRecord::Base belongs_to :place end 

In my controller controller tests, I have the following assert_redirect_to statement

 assert_redirected_to place_review_path(assigns(:place), assigns(:review)) 

but the functional test failed

  1) Failure: test_should_create_review(ReviewsControllerTest) [/test/functional/reviews_controller_test.rb:24]: Expected response to be a redirect to <http://test.host/places/980190962/reviews/980190963> but was a redirect to <http://test.host/places/980190962>. 2) Failure: test_should_destroy_review(ReviewsControllerTest) [/test/functional/reviews_controller_test.rb:47]: Expected response to be a redirect to <http://test.host/places/980190962/reviews.%23%3Creview:0xb74783a0%3E> but was a redirect to <http://test.host/places/980190962/reviews>. 3) Failure: test_should_update_review(ReviewsControllerTest) [/test/functional/reviews_controller_test.rb:39]: Expected response to be a redirect to <http://test.host/places/980190962/reviews/980190962> but was a redirect to <http://test.host/places/980190962>. 

What could be causing this problem?

+4
source share
1 answer

The assert_redirect_to function works as expected. Instead, it looks like your CheckController is redirecting to unexpected places. Assert_redirected_to seems to function properly. And all your failures stem from the fact that the URL to which the action was redirected does not match the URL specified by assert_redirected_to.

More specific:

  • ReviewsController is redirected to place_path(assigns(:place)) . Which is not compatible with the standard Rails workflow. Approval expects place_review_path(assigns(:place), assigns(:review))

  • The ReviewsController redirects to place_reviews_path(assigns(:place)) , which matches the standard Rails workflows. Approval expects place_review_path(assigns(:place), assigns(:review)) . However, since the destroy action removes the assignment identifier (: review), therefore place_review_path(assigns(:place), assigns(:review)) will not be able to create the correct URL that explains the odd characters in the error message.

  • ReviewsController is redirected to place_path(assings(:place)) . Again, this is inconsistent with the standard Rails workflow. Approval expects place_review_path(assigns(:place), assigns(:review))

A short option, if you have not deviated from the Rails conventions, the first and third failures listed in your controller are redirected to an unexpected place. The second unsuccessful statement is incorrect.

+1
source

All Articles