簡體   English   中英

Rails認為Rspec測試期望未達到

[英]Rails view Rspec tests expectations not met

我認為Rspec視圖測試搞砸了,因為測試所尋找的鏈接不是實際視圖的一部分,而是應用程序布局標頭的一部分。

例如,我有以下Rspec代碼:

  describe 'signups/new.html.erb' do

  before :each do
    render template: "signups/new", layout: "layouts/application"
  end

  it 'displays link to request pages not signup page' do
    rendered.should have_link("Make a new request", href: new_request_path)
    rendered.should_not have_link("Add me to the mailing list", href: new_signup_path)
  end

我收到的錯誤是找到了“將我添加到郵件列表”鏈接。 但是我向您保證,在本地服務器中並非如此。

該視圖中的內容如下(再次全部來自布局/應用程序文件,而非來自注冊/新建):

<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <a class="navbar-brand" href="<%= root_path %>">Home</a>
      <a class="navbar-brand" href="<%= new_request_path %>">Make a new request</a>
      <% if current_page?(new_signup_path) == false %>
        <a class="navbar-brand" href="<%= new_signup_path %>">Add me to the mailing list</a>
      <% end %>
    </div>
  </div>
</nav>

基本上是一個If / Then語句,表明當您進入注冊頁面時,不要顯示將用戶發送到注冊頁面的鏈接。 但是Rspec指示存在此鏈接。 有什么想法嗎?

我在想測試不會插值if / then語句,但是不確定如何解決這個問題。

puts page.body放在rendered.should have_link("Make a new request", href: new_request_path)的行之前。 rendered.should have_link("Make a new request", href: new_request_path)我認為您的規范甚至不在頁面上。

像這樣

it 'displays link to request pages not signup page' do
  puts page.body
  rendered.should have_link("Make a new request", href: new_request_path)
  rendered.should_not have_link("Add me to the mailing list", href: new_signup_path)
end

編輯:

重新閱讀該問題,盡管puts page.body幫助測試的好地方,但我以前的建議並沒有那么有用。 我認為可能是因為您正在呈現注冊/新注冊,但這不是路徑,因此不是“當前頁面”。 也許嘗試改為redirect_to。

嘗試visit new_signups_path或任何路徑(檢查我擁有puts.page.body rake routes 。您可能(我認為,但這取決於您的其他測試)擺脫之前的puts.page.body :每條

在測試開始時,您需要使用Capybara導航到注冊頁面。

例如,如果您的注冊路線為/ signup,則可以致電

visit '/signup'

在每個區塊之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM