[rspec-users] [RSpec] implicit receiver for should problem when helper predicate methods are in use

Jarmo Pertman Jarmo.P at gmail.com
Thu May 7 16:34:48 EDT 2009


Okay, I fiddled around a little and made this solution:
module Spec
  module Example
    module Subject
      module ExampleMethods

        def should(matcher=nil)
          if matcher
            self == subject ?
Spec::Expectations::PositiveExpectationHandler.handle_matcher(self,
matcher) : subject.should(matcher)
          else
            self == subject ?
Spec::Expectations::PositiveExpectationHandler.handle_matcher(self) :
subject.should
          end
        end

        def should_not(matcher=nil)
          if matcher
            self == subject ?
Spec::Expectations::NegativeExpectationHandler.handle_matcher(self,
matcher) : subject.should_not(matcher)
          else
            self == subject ?
Spec::Expectations::NegativeExpectationHandler.handle_matcher(self) :
subject.should_not
          end
        end
      end
    end
  end
end

In short, if subject is set as self then I'm calling Kernel::should
method's body directly. I don't know if there's any side effects that
I'm omitting block parameter (although original should and should_not
also doesn't send any blocks).

Any problems that might arise with this patch?

Also, if my original example wasn't real life example enough, then I
will give another one:
require 'watir'

describe "Google" do
   subject {self}

   before :all do
     @b = Watir::Browser.new
     @b.goto "http://www.google.com"
  end

  it "has google written on page" do
    should have_text("Google")
  end

  def has_text? text
    @b.text.include?(text)
  end

end

PS! I changed my poster nick, but I'm still the same "juuser" who made
original thread.

Regards,
Jarmo


More information about the rspec-users mailing list