10

私はruby.railstutorial.org/ruby-on-rails-tutorial-bookに取り組んでいます。Rails 3.2.7、spork、rspec、capybara、launchy、およびいくつかのガードを使用しています:)

私は第3章でテストに関して本当に奇妙な問題を抱えています:

<head>-Tag内にあるものに対してテストが機能していないようです。<title>ヘッドタグの代わりに-tagを-tagの中に入れると、正常<body>に機能します。また、 -tagsを内側の-Tags<h1>の上に置くと機能します。変ですね。<title><head>

私が理解するのを手伝ってください。

例は次のとおりです:ruby.railstutorial.org/chapters/static-pages#code:title_test:

it "should have the right title" do
  visit '/static_pages/home'
  page.should have_selector('title',
                    :text => "Ruby on Rails Tutorial Sample App | Home")
end

エラーメッセージは次のとおりです。

失敗:

1)静的ページホームページのタイトルは「ホーム」である必要があります失敗/エラー:page.should have_selector('title'、:text =>'|ホーム')Capybara :: ExpectationNotMet:css"title"とテキスト"が検索されると予想されます|ホーム」ですが、一致するものはありませんでした。セレクターに一致したが、すべてのフィルターに一致しなかった「」も見つかりました。#。/ spec / requests / static_pages_spec.rb:15:in `block(3 level)in '

それは機能しています:

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

レンダリングされたHTMLファイル:

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | Home</title>
  <!-- some css,js stuff -->
</head>
<body>

<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>

</body>
</html>

ありがとう

編集:githubで使用しているファイルを見つけることができます: https ://github.com/farukg/sample_app/

スペックファイルへのリンク: https ://github.com/farukg/sample_app/blob/master/spec/requests/static_pages_spec.rb

私がしたことの説明:ホームページのコードはあるべき姿です。アバウトページのコードには独自のレイアウトがあり、タイトルタグの上にh1タグがあり、何らかの理由で機能することを示しています。そして最後に、ヘルプページのbodyタグ内にtitleタグがあり、これも機能します。

私は絶対に混乱しています、なぜ私はそのような奇妙な行動をするのですか?

ガードの完全な出力:

> Run all
Bundle already up-to-date
Running all specs
Running tests with args ["--drb", "--colour", "-f", "progress", "-r", "/home/faruk/.rvm/gems/ruby-1.9.3-p125/gems/guard-rspec-1.2.0/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
..FFF.....Neues Fenster in aktueller Browsersitzung erstellt.
.FF..

Failures:

  1) Static pages Home page having application layout should have_selector head title 'Home'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  2) Static pages Home page having application layout should have content 'Home'
     Failure/Error: page.should have_content("Ruby on Rails Tutorial Sample App | Home")
       expected there to be text "Ruby on Rails Tutorial Sample App | Home" in "Sample App This is the home page for the Ruby on Rails Tutorial sample application"
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  3) Static pages Home page having application layout should have css title 'Home'
     Failure/Error: page.should have_css("title", :text => "Ruby on Rails Tutorial Sample App | Home")
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/requests/static_pages_spec.rb:36:in `block (3 levels) in <top (required)>'

  4) Static pages about page with own layout should JUST have_selector head title 
     Failure/Error: page.should have_selector('head title')
     Capybara::ExpectationNotMet:
       expected to find css "head title" but there were no matches
     # ./spec/requests/static_pages_spec.rb:86:in `block (3 levels) in <top (required)>'

  5) Static pages about page with own layout should have_selector head title 'About Us'
     Failure/Error: page.should have_selector('head title',
     Capybara::ExpectationNotMet:
       expected to find css "head title" with text "Ruby on Rails Tutorial Sample App | About Us" but there were no matches
     # ./spec/requests/static_pages_spec.rb:93:in `block (3 levels) in <top (required)>'

Finished in 0.66215 seconds
15 examples, 5 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:20 # Static pages Home page having application layout should have_selector head title 'Home'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Home page having application layout should have content 'Home'
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Home page having application layout should have css title 'Home'
rspec ./spec/requests/static_pages_spec.rb:83 # Static pages about page with own layout should JUST have_selector head title 
rspec ./spec/requests/static_pages_spec.rb:90 # Static pages about page with own layout should have_selector head title 'About Us'
Done.

> Neues Fenster in aktueller Browsersitzung erstellt.
Neues Fenster in aktueller Browsersitzung erstellt.
4

6 に答える 6

7

<title>中にあるから<head>といって表示されません。

これを使用して、私の問題を解決しました:

page.should have_selector 'title', :visible => false

ヒント:同じことを適用して、他の<head>要素をチェックすることができます<meta>。たとえば、Googleに対応していることを確認できます。

于 2013-07-13T06:12:12.057 に答える
6

Githubでプロジェクトを確認しましたが、エッジバージョンのcapybaraを使用しているようです。

Gemfile

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'

Gemfile.lock

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: e561d249555195cdd0e9251246fc75aae876f833
  specs:
      capybara (2.0.0.beta2)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 2.0)
      xpath (~> 1.0.0.beta1)

他に何もないとしても、これがあなたの問題の原因だと思います。(そして確かに、エッジバージョンを使用するようにカピバラを更新した後、私の参照プロジェクトはすべて壊れています)

したがって、 Capybara 2のベータテストを支援することに熱心でない限り、ここにリストされている1.1.2バージョンに固執することをお勧めします。

于 2012-08-01T02:57:45.377 に答える
5

Capybara 2を使用している場合は、次のhave_titleような方法を使用できます。

page.should have_title("Ruby on Rails Tutorial Sample App | Home")

于 2013-06-19T13:00:17.240 に答える
0

これでテストできますか:

page.should have_selector('head title',
                          :text => "Ruby on Rails Tutorial Sample App | Home")
于 2012-07-30T17:42:27.137 に答える
0

これは私にとっても長い間延期した後のことです。stackoverflowのおかげで、説明の直後にスペックファイルの先頭にあるrender_viewsを使用していくつかのテストを修正することができました。私はからテストに合格します

 describe "GET 'home'" do
   it "returns http success" do
     get 'home'
     response.should be_success
 end

しかし、まだレイアウトを使用しないと、:text Capybara :: ExpectationNotMetでこのエラーが発生します:「ホー​​ム| Ruby on Railsチュートリアルサンプルアプリ」というテキストのcss「title」が見つかるはずですが、一致するものはありませんでした。しかし、それを除けば、gemファイルでgem'capybara'、 '1.1.2'を使用した後、私のテストは合格します。しかし、私はまた、アクションマッチとしてルートでこれとしてget'home'を使用します。これがお役に立てば幸いです。

于 2013-02-09T13:13:27.623 に答える
0

今のところ、これを行う最も簡単な方法は、ページhtmlからTitle要素にアクセスすることです。これにより、タイトルが返されます。

page.html.match(/<title>(.*)<\/title>/)[1].to_s

html(!!)で正規表現を使用しているため理想的ではありませんが、機能します。おそらくヘルパー関数を書くのが最善でしょう

# put in spec/support/utilities.rb
def page_title(page)
  page.html.match(/<title>(.*)<\/title>/)[1].to_s
end

次に、テストは次のように記述できます。

page_title(page).should eq( "Ruby on Rails Tutorial Sample App | Home" }
于 2012-11-29T11:44:42.757 に答える