2

给定一个功能测试,例如:

def test_exciting_rails_upgrades
    login(m=users(:manager))

    post :import_users_map_fields, :csv_file => fixture_file_upload('/users.csv', 'text/csv')
    assert flash.empty?
    flash.discard
    # other goodies omitted

end

在 Rails 2.3.2 中没有错误,但是在 2.3.15 中,错误是:

    NoMethodError: {}:Hash 的未定义方法“丢弃”
    /test/functional/broken_upgrades.rb:119:in `test_exciting_rails_upgrades'

为什么是flash一个Hash类而不是一个FlashHash

从源代码来看,2.3.2 和 2.3.15 ActionPack 文件都 lib/action_controller/flash.rb创建了FlashHash类并继承自Hash. 然而,在 2.3.2 和 2.3.15 的这个功能测试中显示的是一个Hash类,而不是 a HashFlash,所以不能对它调用 discard。

其他人可以用 2.3.15 重现此错误flash.discard吗?

4

1 回答 1

1

这里有两个测试用例可以用来证明 ActionController 根据它是否已经设置来改变'flash'的类型。

在我的应用程序中,除非您已登录,否则您无法看到 :index,因此在 test_flash_is_now_a_flashhash 中,您会看到后端正确设置了 flash,而在 test_flash_is_a_plain_hash 中却没有。

def test_flash_is_a_plain_hash
  login(users(:permitted_user))
  get :index
  assert flash.instance_of?(Hash)
end

def test_flash_is_now_a_flashhash
  get :index
  assert_redirected_to :controller => "login"
  assert flash.instance_of?(ActionController::Flash::FlashHash)
end

您可以在 ActionController::TestRequest 代码中亲自看到这一点:

def flash
    session['flash'] || {}
end

更新:这已在 Rails 分支2-3-stable中修复

于 2013-01-17T21:31:57.193 回答