簡體   English   中英

Rspec測試應該通過但是失敗

[英]Rspec test should pass but fails

我從邁克爾哈特爾書中得到了這個測試:

require 'spec_helper'
  describe "Static pages" do
    let(:base_title) { "Ruby on Rails Tutorial Sample App" }

    describe "Home page" do
      it "should have the h1 'Sample App'" do
        visit '/static_pages/home'
        page.should have_selector('h1', :text => 'Sample App')
      end

      it "should have the title 'Home'" do
        visit '/static_pages/home'
        page.should have_selector('title', :text => "#{base_title} | Home")
      end
  end
end

並且觀點:

<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application.
</p>

當我運行測試時,它說:

....

Finished in 1.91 seconds
4 examples, 0 failures

Randomized with seed 42247

.F...

Failures:

  1) Static pages Home page should have the title 'Home'
     Failure/Error: page.should have_selector('title', :text => "#{base_title} | Home")
       expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App | Home"}) to return true, got false
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 1.91 seconds
5 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the title 'Home'

Randomized with seed 17491

但它應該通過,因為當我在瀏覽器中查看頁面時,標題是:Ruby on Rails Tutorial Sample App | 示例應用程序,這是正確的!

確保你在Gemfile中使用capybara 1.1.2。 從2.0 capybara開始不適用於標題測試(https://github.com/jnicklas/capybara/issues/844)

...
group :test do
  gem 'capybara', '1.1.2'
end

目前,你應該做@dimuch的建議,並確保你指定Michael Hartl在教程(1.1.2)中使用的相同Capybara版本。

如果您希望將來升級到Capybara 2.0並保留測試標題,請查看此StackOverflow答案 ,以獲取創建RSpec匹配器的指南,該指南將執行您期望的操作。

使用capubara 2.0你應該使用

page.should have_title("The title")

但是,如果你不添加,那么在工作中

<style type="text/css">head, head title { display: block }</style>

到您的application.html

page.title # => "The title"
page.has_title?("The title") # => true
page.should have_title("The title")

我一直在使用以下內容,他們一直在發布綠色。 我刪除了have_selector並使用了have_title。

it { should have_title( full_title('Sign up') ) }

- 和 -

it { should have_title(user.name) }

這是與水豚2.2.0。

暫無
暫無

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

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