Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Checking containment of a substring in a string.

I think I'm using the right syntax as documented here, but it's not working for me. What am I missing?

>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> s.include?('tat')
=> true
>> s.should include('tat')
TypeError: wrong argument type String (expected Module)
    from (irb):4:in `include'
    from (irb):4
    from /usr/bin/irb:12:in `<main>'
share|improve this question
add comment

2 Answers

Should expects a matcher object so simplest way to answer your question would be:

>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> s.should RSpec::Matchers::BuiltIn::Include.new('tat')
=> true

Lets talk about different matcher for a while eq (because there is something about include)

>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> s.should eq('potato')
NoMethodError: undefined method `eq' for main:Object

To get eq to work we can include RSpec::Matchers module(method definitions start at line 193)

>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> include RSpec::Matchers
>> s.should eq('potato')
=> true

So what you are missing is to extend your object with RSpec::Matchers module methods or simply passing a matcher to should method.

Problem with include matcher in IRB is still present(not 100% sure why):

>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> include RSpec::Matchers
>> s.should include('tat')
TypeError: wrong argument type String (expected Module)

It might have something to do with working in a context of a main object:

>> self
=> main
>> self.class
=> Object
>> Object.respond_to(:include)
=> false
>> Object.respond_to(:include, true) #check private and protected methods as well
=> true

Object have a private method include. Include method from RSpec::Matchers never gets chance to get called. If you wrap it in a class that will include RSpec::Matchers everyting should be working.

Rspec does it with MiniTest::Unit::TestCase RSpec::Matchers (line 168)

share|improve this answer
    
No, doesn't seem to be IRB glitch because it happens when executing from a script as well. –  wim Mar 12 '13 at 2:55
    
I've done some research and change my answer. –  Kocur4d Mar 12 '13 at 13:24
add comment

You need a context or it block around the expectation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.