重定向后的Flash消息功能测试 [英] Functional tests for flash messages after redirect

查看:107
本文介绍了重定向后的Flash消息功能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何通过功能测试来检查Flash消息.我的问题与redirect_to有关,因为我对flash[:error]的测试通过了.这是我的create动作的样子:

I'm not sure how to check flash messages with functional tests. My problem has something to do with redirect_to because my test for flash[:error] DOES pass. Here is what my create action looks like:

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, flash: { success: "Welcome!" }
  else
    flash.now[:error] = "Signup failed:"
    render 'new'
  end
end

我对flash[:error]进行了通过测试:1)检查flash[:error]是正确的值,2)检查页面的flash div,3)获取新页面并检查是否已显示flash消息已清除.我试图对flash[:success]做一个类似的测试,但是当它试图在页面上找到div时却失败了.这是失败的测试:

I have a passing test for flash[:error] that 1) checks that flash[:error] is the correct value, 2) checks the page for the flash div, and 3) gets a new page and checks that the flash message has been cleared. I tried to make an analogous test for flash[:success] but it is failing when it tries to find the div on the page. Here is the failing test:

  should "show flash[:success] message" do
    post :create, user: { name: "valid user",
                          password: "userpw",
                          password_confirmation: "userpw",
                          email: "a@b.com" }        
    assert_redirected_to user_path(assigns(:user)), "user isn't being redirected to their page after registration"
    assert_equal flash[:success], "Welcome!", "flash[:success] isn't being set properly"
    assert_select "div.alert-success", flash[:success]
    get :new
    assert flash.empty?, "there shouldn't be any flash messages left after getting new page"
  end

当我在浏览器中手动测试它时,它的行为符合预期,但是我不确定如何编写测试来反映这一点.前两个断言通过了(因此测试识别出它已正确重定向,并且flash[:success]被设置为"Welcome!").基于此,我认为assert_select语句将能够找到div,但是如果失败并显示以下错误消息:

When I test this manually in a browser, it behaves as expected, but I'm not sure how to write the tests to reflect this. The first two assertions are passing (so the test recognizes that it is being redirected correctly, and that flash[:success] is being set to "Welcome!"). Based on that, I would think the assert_select statement would be able to find the div, but if fails with this error message:

Expected at least 1 element matching "div.alert-success", found 0.

所以我尝试在assert_select语句之前添加puts @response.body,该语句显示为:

So I tried adding puts @response.body right before the assert_select statement, which printed this:

<html><body>You are being <a href="http://test.host/users/3">redirected</a>.</body></html>

这说明了为什么找不到flash div的原因,但是我不确定如何完成"重定向,以便它使用flash div呈现正确的视图.如果我在assert_select语句之前添加get语句,则测试通过,因为现在@response.body具有flash div.但是,这似乎不是有效的测试,因为它不能反映我的应用程序的行为-用户不必发出新请求即可查看Flash消息.

This explains why it couldn't find the flash div, but I'm not sure how to "finish" the redirect so that it renders the correct view with the flash div. If I add a get statement right before the assert_select statement, the test passes, because now @response.body has the flash div. However, this doesn't seem like an effective test because it doesn't reflect the behavior of my application - the user doesn't have to make a new request to see the flash message.

这也不保证Flash消息会显示在正确的页面上.如果我使用语句get :new,则声明仍将通过,这意味着Flash消息将出现在users#new视图中.即使我使用

It also doesn't guarantee that the flash message shows up on the correct page. If I use the statement get :new, the assertion still passes, which implies that the flash message would be present on the users#new view. Even if I use

get :show, id: assigns(:user).id  

确保Flash消息显示在正确的页面上,这仍然感觉不对,因为1)现在是success响应,而不是redirect响应; 2)它仍然没有解释为什么我必须手动请求任何页面才能找到Flash div-根据 docs ,"Flash是会话的特殊部分,每个请求均已清除."

to make sure the flash message shows up on the correct page, this still feels wrong because 1) now it is a success response instead of a redirect response, and 2) it still doesn't explain why I have to manually request ANY page to find the flash div - according to the docs, "The flash is a special part of the session which is cleared with each request."

无论如何,是否有一种方法可以测试重定向后是否能正确显示Flash消息?

Anyway, is there a way to test that a flash message shows up correctly after a redirect?

推荐答案

我从POV回答了这个问题,即您尝试测试的内容将在其他地方得到更好的测试.如果您在设置测试时遇到很多麻烦,很可能有更好的方法来测试它了:)

I'm answering this question from the POV that what you're trying to test would be better tested elsewhere. If you're having a lot of trouble setting up the test, odds are there's a better way to test it :)

  1. 测试是否清除了Flash消息-Flash消息内置在Rails中,因此测试框架为您处理的内容没有多大意义.

  1. Testing that flash messages get cleared - flash messages are built into Rails, so it doesn't make a lot of sense to test something that the framework is handling for you.

测试元素是否显示在页面上-视图规范不属于控制器.这是控制器角色的描述:

Testing that elements show up on the page - view specs don't belong in controllers. Here is a description of the role of the controller:

因此,可以将控制器视为模型和模型之间的中间人. 意见.它使模型数据可用于视图,以便可以显示 数据发送给用户.

A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user.

该控制器应该使数据可用于视图,而不是指定如何呈现数据.想象一下,如果您更改了CSS类的名称,那么您还必须更新控制器规范.

The controller is supposed to make data available to the view, not to specify how the data gets rendered. Imagine if you changed the name of your CSS class, then you'd have to also update your controller specs.

如果您真的想测试是否显示Flash消息,我将使用集成测试.

If you really want to test that flash messages show up, I would use an integration test.

这篇关于重定向后的Flash消息功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆