本文介绍了Rspec测试应该通过但失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我从本书中进行了这项测试:

I have this test from michael hartl book:

require 'spec_helper'
  describe "Static pages" do
    let(:base_title) { "Ruby on Rails Tutorial Sample App" }

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

      it "should have the title 'Home'" do
        visit '/static_pages/home'
        page.should have_selector('title', :text => "#{base_title} | Home")
      end
  end
end

视图:

<% provide(:title, 'Home') %>
<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>

我运行测试时说:

....

Finished in 1.91 seconds
4 examples, 0 failures

Randomized with seed 42247

.F...

Failures:

  1) Static pages Home page should have the title 'Home'
     Failure/Error: page.should have_selector('title', :text => "#{base_title} | Home")
       expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App | Home"}) to return true, got false
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 1.91 seconds
5 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the title 'Home'

Randomized with seed 17491

但是它应该通过,因为当我在浏览器中查看页面时,标题是:Ruby on Rails Tutorial Sam ple App |

But It should pass, because when I view the page in browser the title is: Ruby on Rails Tutorial Sample App | Sample App, which is correct!

推荐答案

请确保您在Gemfile中使用了水豚1.1.2。从2.0开始,capybara不适用于标题测试(https://github.com/jnicklas/capybara/issues/844)

Make sure you're using capybara 1.1.2 in your Gemfile. Starting from 2.0 capybara does not works for title testing (https://github.com/jnicklas/capybara/issues/844)

...
group :test do
  gem 'capybara', '1.1.2'
end

这篇关于Rspec测试应该通过但失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:12