Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RSpec expectations in irb

I'd want to use [1,2,3].should include(1) in irb. I tried:

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

But it doesn't work though it's a valid case. How can I use [1,2,3].should include(1)?

like image 570
Andrei Botalov Avatar asked Feb 07 '13 10:02

Andrei Botalov


1 Answers

You are close, but calling include on top-level you will be calling Module#include. To get around it you need to remove the original include method so that RSpec's include gets called instead.

First let's figure out where the system include comes from:

> method :include
=> #<Method: main.include>

Ok. It looks like it's defined in main. This is the Ruby top-level object. So let's rename and remove the original include:

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

Now we can get down to business:

> require 'rspec'
> inc RSpec::Matchers
> [1,2,3].should include(1)
=> true
like image 114
Casper Avatar answered Nov 14 '22 02:11

Casper