首页 > 解决方案 > RSpec Controller 测试屏幕上的文本失败

问题描述

我在 h1 元素中有一个带有“设置”的索引视图。

<div id="settings-main-container">
  <div id="settings-main-title">
    <h1 id="settings-title">Settings</h1>
  </div>
  <%=link_to admin_clients_index_path do%>
    <div class="settings-button">
      Select Active Client
    </div>
 <%end%>
 <%=link_to admin_settings_assign_users_to_clients_index_path do%>
    <div class="settings-button">
        Assign Users to Clients
    </div>
 <%end%>
 <%=link_to destroy_user_session_path, method: :delete do%>
   <div class="settings-button">
     Logout
   </div>
 <%end%>
</div>

我有我的测试设置

describe "GET index" do
  render_views
  ...

  it "should have text on the screen" do
    get :index
    response.should have_text("Settings")
  end
end

我不断收到错误

Failure/Error: response.should have_text("Settings")
   expected to find text "Settings" in "#"

任何帮助将不胜感激。

标签: ruby-on-railsrubyrspec

解决方案


response包含标头、响应代码和响应正文等内容。因此,如果您想检查渲染视图 - 您应该检查response.body

response.body.should have_text("Settings")

推荐阅读