How to use RSpec expectations in irb
Asked Answered
M

1

11

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)?

Mannequin answered 7/2, 2013 at 10:38 Comment(0)
I
15

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
Importunate answered 7/2, 2013 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.