2013-02-07 43 views
10

我想在irb中使用[1,2,3].should include(1)。我想:如何在irb中使用RSpec期望

~$ 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 `<main>' 

不過,這並不雖然it's a valid case工作。我怎樣才能使用[1,2,3].should include(1)

回答

13

您已經接近,但在頂級調用include您將撥打Module#include。爲了解決它,你需要刪除原始的include方法,以便RSpec的include被調用。

首先,讓我們找出其中的系統include來自:

> method :include 
=> #<Method: main.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