如何在irb中使用RSpec期望值

我想用[1,2,3].should include(1) 。在irb中[1,2,3].should include(1) 。 我试过了:

 ~$ irb 1.9.3p362 :001 > require 'rspec/expectations' => true 1.9.3p362 :002 > include RSpec::Matchers => Object 1.9.3p362 :003 > [1,2,3].should include(1) TypeError: wrong argument type Fixnum (expected Module) from (irb):3:in `include' from (irb):3 from /home/andrey/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `' 

但它不起作用虽然它是一个有效的案例 。 我如何使用[1,2,3].should include(1)

你很亲密,但是在顶级调用include你将调用Module#include 。 要绕过它,您需要删除原始的include方法,以便调用RSpec的include

首先让我们弄清楚系统include来源:

 > method :include => # 

好。 它看起来像是在main定义的。 这是Ruby顶级对象。 所以让我们重命名并删除原始包含:

 > class << self; alias_method :inc, :include; remove_method :include; end 

现在我们可以开展业务了:

 > require 'rspec' > inc RSpec::Matchers > [1,2,3].should include(1) => true