【问题标题】:RoR rspec test failsRoR rspec 测试失败
【发布时间】:2014-03-13 05:31:23
【问题描述】:

这里是视图:

<!-- 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/' %>

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

这是测试:

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 

【问题讨论】:

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


    【解决方案1】:

    问题来了

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

    这不是正确的 HTML 注释语法。结束评论标签应该是--&gt; 而不是--!&gt;。使用以下内容。

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

    您的测试失败了,因为您的 HTML cmets 没有终止,因此您的 H1 丢失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多