0

刚开始使用Test::Unit但在确定重定向测试时遇到问题:

  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

引发以下错误:

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

文档说明以下内容:

assert_redirected_to(options = {}, message=nil) 公共

断言传入的重定向选项与最新操作中调用的重定向选项匹配。这种匹配可以是部分的,这样 assert_redirected_to(:controller => "weblog") 也会匹配 redirect_to(:controller => "weblog", :action => "show") 等的重定向。

assert_redirected_to controller: 'carts'导致更彻底的失败:

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)

如果文档是正确的,我错过了什么?如果不是,无论匹配的ID如何,我都必须测试重定向的替代方法是什么?

4

1 回答 1

0

似乎文档assert_redirected_to并不完全正确,但部分问题与在设置阶段(@line_item创建时)与销毁阶段相比,cart_id 不同。所以解决方法是修改夹具/控制器,使其在销毁时具有相同的 cart_id。它不能解决真正的问题,assert_redirected_to但至少测试会通过。

于 2012-10-24T21:45:34.277 回答