Just starting out in using Test::Unit but having a problem nailing down a redirect test:
test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete :destroy, id: @line_item
end
assert_redirected_to controller: 'carts', action: 'show'
end
which throws the following error:
Failure: Expected response to be a redirect to <http://test.host/carts/980190962> but was a redirect to <http://test.host/carts/980190963>.
test_should_destroy_line_item(LineItemsControllerTest)
test/functional/line_items_controller_test.rb:47:in `block in <class:LineItemsControllerTest>'
The docs state the following:
assert_redirected_to(options = {}, message=nil) public
Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that assert_redirected_to(:controller => “weblog”) will also match the redirection of redirect_to(:controller => “weblog”, :action => “show”) and so on.
but assert_redirected_to controller: 'carts' leads to an even more outright failure:
Failure: Expected response to be a redirect to <http://test.host/carts> but was a redirect to <http://test.host/carts/980190963>.
test_should_destroy_line_item(LineItemsControllerTest)
If the documentation is correct, what am I missing? If it is not, what alternatives do I have to test the redirect regardless of a matching id?
It appears that the documentation for
assert_redirected_tois not entirely correct, but part of the problem had to do with the cart_id being different at the setup stage (when@line_itemis created) as compared to the stage where it is destroyed. So the workaround is to modify the fixture/controller such that it will have the same cart_id at the time of destruction. It doesn’t solve the real problem withassert_redirected_tobut at least the test will pass.