ruby-on-rails – 测试操作时如何在flash中放一个值
发布时间:2020-12-16 21:40:19 所属栏目:百科 来源:网络整理
导读:我试图测试一个需要存储在flash中的值的动作. def my_action if flash[:something].nil? redirect_to root_path if flash[:something] return end # Do some other stuffend 在我的测试中,我做的事情就像: before(:each) do flash[:something] = "bob"endit
我试图测试一个需要存储在flash中的值的动作.
def my_action if flash[:something].nil? redirect_to root_path if flash[:something] return end # Do some other stuff end 在我的测试中,我做的事情就像: before(:each) do flash[:something] = "bob" end it "should do whatever I had commented out above" do get :my_action # Assert something end 我遇到的问题是,flash中没有my_action内的值.我猜这是因为没有请求实际上发生. 有没有办法设置闪光灯这样的测试? 解决方法
问题是,使用flash哈希的方式,这意味着它只能用于下一个请求.为了将flash哈希值设置为测试值,您可以这样写:
def test_something_keeps_flash @request.flash[:something] = 'bar' xhr :get,:my_action assert_response :success // Assert page contents here end 这确保您可以检查您的操作的逻辑.因为现在将正确设置闪存散列,请输入您的my_action并对闪存哈希进行检查. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |