Take the 2-minute tour ×

I'm using watir-cucumber for test automation. I wrote following method in a separate .rb, this method is not in step definitions.

 def has_logged_in?
   $browser.text.should include("t788")

When I call this method from step definition this error comes,

 wrong argument type String (expected Module) (TypeError)

the same code works fine in step definitions. I searched around and found out that include method is used to include module but that is ruby-include method and should include comes under rspec\expectations. So how do I call should include method outside step definition like above. I'm using watir-cucumber on linux

share|improve this question
Where and why you wrote that method? –  Ismael Abreu Oct 22 '12 at 13:35
you could do text.should =~ /t788/ just as well –  froderik Oct 22 '12 at 14:43
@IsmaelAbreu I wanted to test if user has logged in so instead of writing it for every step I would like to call this function and then it will decide step is passed or failed. This is to avoid repetation in code –  Uday Swami Oct 23 '12 at 6:31
@froderik it isn't working –  Uday Swami Oct 23 '12 at 6:32
add comment

2 Answers

The include method that you want is in the RSpec::Matchers module.

If your has_logged_in? method is in a class (not part of main), you can include the RSpec::Matchers module in your class. This would give you access to the include method.

So your class would look like:

class YourClass
    include RSpec::Matchers

    def has_logged_in?
        $browser.text.should include("t788")

Note: I have not had to do this before, but from a quick check, it does work as long as the RSpec::Matchers are included in a class rather than the main. Including it in the main does not appear to do anything (ie include continues to call the standard include module method). I did not explore to see if if there are any negative side effects of doing this.

share|improve this answer
its not working –  Uday Swami Oct 26 '12 at 6:29
@UdaySwami is it the same exception you saw before? Can you share your entire .rb file? –  Justin Ko Oct 31 '12 at 17:28
add comment

In your gem file:

gem 'rspec', "1.3.2", :require => "spec/expectations"

Or in your env.rb for RSpec 1.X.X:

require 'spec/expectations'

Or in your env.rb for RSpec 2.X:

require 'rspec/expectations'
share|improve this answer
I did that require 'rspec/expectations' in my code –  Uday Swami Oct 23 '12 at 6:25
add comment

Your Answer


