3

私はマイケル・ハートルの本からこのテストを持っています:

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 チュートリアル サンプル アプリ | サンプルアプリ、正解!

4

4 に答える 4

5

Gemfile で capybara 1.1.2 を使用していることを確認してください。2.0以降のカピバラは、タイトルのテストでは動作しません (https://github.com/jnicklas/capybara/issues/844)

...
group :test do
  gem 'capybara', '1.1.2'
end
于 2013-01-02T12:51:54.417 に答える
3

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")
于 2013-04-25T07:51:13.223 に答える
3

とりあえず、@dimuch が提案することを実行し、Michael Hartl がチュートリアル (1.1.2) で使用するのと同じカピバラ バージョンを指定するようにしてください。

将来的に Capybara 2.0 にアップグレードし、タイトルのテストを保持したい場合は、この StackOverflow の回答を見て、期待どおりの動作をする RSpec マッチャーを作成するためのガイドを確認してください。

于 2013-01-02T14:07:38.663 に答える