首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在rspec中,水豚匹配器总是求值为TRUE。为什么?

在rspec中,水豚匹配器总是求值为TRUE。为什么?
EN

Stack Overflow用户
提问于 2013-01-14 00:59:59
回答 1查看 517关注 0票数 1

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

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

到目前为止,我唯一的线索是在第二个规范中使用了应该have_content('Bogus'),它报告说Proc是预期的……

./spec/controller/hello_world_spec.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-15 01:41:26

原来罪魁祸首在Gemfile中同时有"bacon“和"rspec”。水豚被介绍到一个项目中,该项目利用bacon作为测试套件,正在尝试的示例是rspec。一旦培根从捆绑的宝石中移除,水豚规格就会按照文档运行。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14305820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档