RSpec控制器测试无法在屏幕上显示文本

时间:2018-08-28 15:02:06

标签: ruby-on-rails ruby rspec

我在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 "#"

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

response包含标头,响应代码和响应正文之类的内容。因此,如果要检查呈现的视图,则应检查response.body

response.body.should have_text("Settings")