测试操作时如何在 Flash 中输入值

发布于 2024-12-10 11:36:42 字数 468 浏览 1 评论 0原文

我正在尝试测试需要存储在闪存中的值的操作。

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 中没有值。我猜这是因为实际上没有发生任何请求。

有没有办法为这样的测试设置闪存?

I'm trying to test an action that needs a value stored in flash.

def my_action
  if flash[:something].nil?
    redirect_to root_path if flash[:something]
    return
  end

  # Do some other stuff
end

In my test I do something like:

before(:each) do
  flash[:something] = "bob"
end

it "should do whatever I had commented out above" do
  get :my_action
  # Assert something
end

The problem I'm running into is that flash has no values inside of my_action. I'm guessing this is because no request actually happens.

Is there a way to set flash up for a test like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

留一抹残留的笑 2024-12-17 11:36:42

我必须解决类似的问题;
我有一个控制器操作,根据哈希条目的值,在完成时重定向到两个路径之一。
对于上面的示例,我发现有效的规范测试是:

it "should do whatever I had commented out above" do
  get :my_action, action_params_hash, @current_session, {:something=>true}
  # Assert something
end

@current_session 是具有会话特定内容的哈希;我正在使用 authlogic。
我在[测试 Rails 应用程序指南[1]< 中找到了关于使用 get 的第四个参数的 flash /a>)。我发现同样的方法也适用于删除;我想其他人也是如此。

I had to solve a similar issue;
I had a controller action that redirected to one of two paths at completion depending of the value of a hash entry.
The spec test that I found worked, for your example above, was:

it "should do whatever I had commented out above" do
  get :my_action, action_params_hash, @current_session, {:something=>true}
  # Assert something
end

@current_session is a hash with session specific stuf; I'm using authlogic.
I found about using the fourth argument of get for the flash in [A Guide to Testing Rails Applications[1]). I found that the same approach also works for delete; and I presume all others.

欲拥i 2024-12-17 11:36:42

以下内容对我来说适用于 RoR 4.1:

flash_hash = ActionDispatch::Flash::FlashHash.new
flash_hash[:error] = 'an error'
session['flash'] = flash_hash.to_session_value

get :my_action

the following worked for me with RoR 4.1:

flash_hash = ActionDispatch::Flash::FlashHash.new
flash_hash[:error] = 'an error'
session['flash'] = flash_hash.to_session_value

get :my_action
难忘№最初的完美 2024-12-17 11:36:42

问题是,按照您的方式使用闪存哈希意味着它只能用于下一个请求。为了将闪存哈希设置为测试的值,您可以编写如下内容:

def test_something_keeps_flash
  @request.flash[:something] = 'bar'
  xhr :get, :my_action
  assert_response :success
  // Assert page contents here
end

这确保您可以检查操作的逻辑。因为它现在将正确设置闪存哈希,所以输入您的 my_action 并对闪存哈希执行检查。

The problem is that using the flash hash the way you do means it only becomes avaialble for the next request. In order to set the flash hash to a value for your test, you could write something like this:

def test_something_keeps_flash
  @request.flash[:something] = 'bar'
  xhr :get, :my_action
  assert_response :success
  // Assert page contents here
end

This ensures that you can check the logic of your action. Because it will now set the flash hash properly, enter your my_action and perform the check on the flash hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文