RoR rspec测试失败

时间:2014-03-13 05:31:23

标签: ruby-on-rails rspec railstutorial.org

以下是观点:

<!-- Only have to provide what is not provided in the layout. 
    The layour is the skeleton. --!>
<div class="center hero-unit">
    <h1>Sample App</h1>

    <h2>
    This is the home page for 
    <%= link_to "Ruby on Rails Tutorial", 'http://railstutorial.org/' %>
    sample application! 
    </h2>

    <%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://railstutorial.org/' %>

之前我遇到过类似的问题,这与测试访问错误的链接有关。我在主页上使用了inspect来确保标题确实在那里,即使我的测试失败了。

以下是测试:

require 'spec_helper'

describe "Static pages" do

  subject { page }

  shared_examples_for "all static pages" do
    it { should have_selector('h1', text: heading) }
    it { should have_title(full_title(page_title)) }
  end

  describe "Home page" do
    before { visit root_path }
    let(:heading)    { 'Sample App' }
    let(:page_title) { '' }

    it_should_behave_like "all static pages"
    it { should_not have_title('| Home') }
  end
end

这是错误:

Failures:

  1) Static pages Home page it should behave like all static pages 
     Failure/Error: it { should have_selector('h1', text: heading) }
       expected #has_selector?("h1", {:text=>"Sample App"}) to return true, got false
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:17
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.44614 seconds
30 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:8 # Static pages Home page it should behave like all static pages 

1 个答案:

答案 0 :(得分:1)

问题出在这里

<!-- Only have to provide what is not provided in the layout. 
    The layour is the skeleton. --!>

这不是HTML的正确评论语法。结束评论标记应为-->而不是--!>。使用以下内容。

<!-- Only have to provide what is not provided in the layout. 
    The layour is the skeleton. -->

您的测试失败,因为您的HTML注释未终止,因此您的H1丢失。