RSpec/Capybara测试has_selector难题

SSO*_*SOK 6 tdd bdd rspec ruby-on-rails capybara

这是来自Michael Hartl第二版的Ruby on Rails教程.第3版(当前最新版)没有用RSpec测试,所以我决定使用这本书.

我已经阅读了用户的解决方法并知道有不同的方法来编写测试以使其工作但我想使用have_selector来保持代码的一致性.任何解释/建议都会非常有帮助.我不明白为什么下面的测试通过了h1元素而不是title元素:

spec.rb:

describe "Static pages" do 

  describe "Home page" do

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

  it "should have the title 'Home'" do
    visit '/static_pages/home'
    expect(page).to have_selector('title', 
                         :text => "Ruby on Rails Tutorial Sample App | Home")
  end
end
Run Code Online (Sandbox Code Playgroud)

home.html的:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
<body>
  <h1>Sample App</h1>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Pra*_*thy 9

尝试:

expect(page).to have_selector('title', 
                     :text => "Ruby on Rails Tutorial Sample App | Home",
                     :visible => false)
Run Code Online (Sandbox Code Playgroud)

由于title标签位于<head>元素中,因此被视为隐藏.指定:visible => false包括要在have_selector匹配器中考虑的那些标记.