为什么我在RSpec中遇到这些故障?

时间:2013-08-25 07:28:20

标签: ruby-on-rails testing rspec

运行'bundle exec rspec spec / requests / static_pages_spec.rb时出现以下错误:

9 examples, 2 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:56 # Static pages Contact page should have the title 'Contact page'
rspec ./spec/requests/static_pages_spec.rb:51 # Static pages Contact page should have the content 'Contact page'

我无法弄清楚如何让这两个测试通过。

尝试在这里学习,学习,非常新(我相信这是代码):

 describe "Contact page" do

    it "should have the content 'Contact'" do
      visit '/static_pages/contact'
      expect(page).to have_content('Contact')
    end

    it "should have the title 'Contact'" do
      visit '/static_pages/contact'
      expect(page).to have_content("Ruby on Rails Tutorial Sample App | Contact")
    end
  end
end

此外,html文件:

<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
  Contact Ruby on Rails Tutorial about the sample app at the
  <a href="http://railstutorial.org/contact">contact page</a>.
</p>

1 个答案:

答案 0 :(得分:1)

您期望使用have_content标题并期望内容为have_title。

尝试

expect(page).to have_title('Contact')

expect(page).to have_content("Ruby on Rails Tutorial Sample App | Contact")

实际上你需要重新考虑最后一个,因为这不是你在视图中的内容,但你明白了。