views:

166

answers:

1

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 reviews controller test I have the following assert_redirect_to statement

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

but functional test fail with the following error

  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&gt; but was a redirect to <http://test.host/places/980190962&gt;.

  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&gt; but was a redirect to <http://test.host/places/980190962/reviews&gt;.

  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&gt; but was a redirect to <http://test.host/places/980190962&gt;.

What could be causing this problem?

+1  A: 

The assert_redirect_to is working as expected. Instead, it looks like your ReviewsController is redirecting to unexpected places. assert_redirected_to appears to be functioning as it should. And all your failures stem from the fact that the url the action was redirected to does not match the url given to assert_redirected_to.

More specifically:

  1. ReviewsController is redirecting to place_path(assigns(:place)). Which is inconsistent with the standard Rails workflow. The assert expects place_review_path(assigns(:place), assigns(:review))

  2. ReviewsController is redirecting to place_reviews_path(assigns(:place)) which is consistent with the standard Rails workflow. The assert expects place_review_path(assigns(:place), assigns(:review)). However because destroy action removes the id of assigns(:review), so place_review_path(assigns(:place), assigns(:review)) will fail to generate a proper url which explains the odd characters in the error message.

  3. ReviewsController is redirecting to place_path(assings(:place)). Again this is inconsistent with the standard Rails workflow. The assert expects place_review_path(assigns(:place), assigns(:review))

The short version is, unless you've strayed from Rails conventions, the first and third failures listed your controller are redirecting to an unexpected place. The second failed assertion is just wrong.

EmFi
So I was using redirect_to(@place, @review) in the controller.The correct form seems to be redirect_to([@place, @review]).It works now. Thanks!
Maulin