1

capybara 和 rspec 发生了一些奇怪的事情,我在 Ruby 1.9.3、Padrino 0.10.7、rspec 2.11.0、capybara 2.0.2 上进行了设置。

一个基本的 Padrino 项目设置了 haml 和 rspec(还没有自定义代码!),而不仅仅是加载一个“/”页面(我验证它确实按以下规范中的“puts page.content”呈现)。这是简单的规格。“Bogus”不存在,但“Home”确实......请注意,当我进入控制台时,预期的真/假是正确的,但由于某种原因,匹配器没有正确看到真/假。

到目前为止,我的一个线索在于使用 should have_content('Bogus') 的第二个规范,它报告说 Proc 是预期的......

./spec/controllers/hello_world_spec.rb

require 'spec_helper'

require 'capybara'
require 'capybara/rspec'

describe 'The HelloWorld App', :type => :feature do

  context "per documentation" do 
    it "has bogus content" do
      visit '/'
      page.has_content?('Bogus')
    end

    it "does not have bogus content" do
      visit '/'
      page.should have_content("Bogus")
    end
  end

  context "should tests" do 
    it "has bogus content" do
      visit '/'
      page.has_content?('Bogus').should == true
    end

    it "does not have bogus content" do
      visit '/'
      page.has_content?('Bogus').should == false
    end
  end

  context "variables" do 
    it "has bogus content" do
      visit '/'
      result = page.has_content?('Bogus')
      puts result
      result.should == true
    end

    it "has Home content (expect TRUE!)" do
      visit '/'
      result = page.has_content?('Home')
      puts result
      result.should == true
    end

    it "does not have bogus content" do
      visit '/'
      result = page.has_content?('Bogus')
      puts result
      result.should == false
    end
  end
end

spec_helper.rb

PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")

def app
  ##
  # You can handle all padrino applications using instead:
  Padrino.application
  # Askme.tap do |app|
  # end
end

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
  Capybara.app = app
end

输出:

11:40:57:website >> bundle exec rspec spec/app/controllers/hello_world_controller_spec.rb 
WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8

The HelloWorld App
  per documentation
    has bogus content
    does not have bogus content (FAILED - 1)
  should tests
    has bogus content
    does not have bogus content
  variables
false
    has bogus content
true
    has Home content (expect TRUE!)
false
    does not have bogus content

Failures:

  1) The HelloWorld App per documentation does not have bogus content
     Failure/Error: page.should have_content("Bogus")
     TypeError:
       wrong argument type Capybara::RSpecMatchers::HaveText (expected Proc)
     # ./spec/app/controllers/hello_world_controller_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 1.66 seconds
7 examples, 1 failure

Failed examples:

rspec ./spec/app/controllers/hello_world_controller_spec.rb:14 # The HelloWorld App per documentation does not have bogus content
4

1 回答 1

0

原来罪魁祸首是在 Gemfile 中同时包含“bacon”和“rspec”。Capybara 被介绍到一个使用 bacon 作为测试套件的项目中,并且正在尝试的示例是 rspec。一旦 bacon 从捆绑的 gem 中移除,capybara 规范就会按照文档运行。

由于 bacon 脚本或多或少按原样在 rspec 下运行,因此项目决定是删除 bacon 并使用 rspec 进行测试套件,并对 bacon 脚本进行细微调整以在 rspec 下运行所有​​规范。

于 2013-01-14T17:41:26.413 回答