在rspec中,capybara匹配器总是eval为TRUE。为什么?

时间:2013-01-13 16:59:59

标签: ruby rspec capybara padrino

与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”确实...请注意,当我放入控制台时,预期的true / false是正确的,但由于某种原因,匹配器没有正确看到真/假。 / p>

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

./规格/控制器/ 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

1 个答案:

答案 0 :(得分:0)

原来,罪魁祸首是在Gemfile中同时拥有“bacon”和“rspec”。 Capybara正在介绍一个利用培根进行测试套件的项目,正在尝试的例子是rspec。一旦培根从捆绑的宝石中移除,根据文档运行水豚规格。

由于培根脚本在rspec下或多或少地运行,项目决定是删除培根并使用rspec进行测试套件,并对培根脚本进行小调整以运行rspec下的所有规格。