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

David Chelimsky dchelimsky at gmail.com
Thu May 7 17:02:55 EDT 2009


On Thu, May 7, 2009 at 1:34 PM, Jarmo Pertman <Jarmo.P at gmail.com> wrote:
> 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?

The first problem is the dependency on subject, which is a construct
from rspec's example groups. This would make rspec's matchers unusable
outside rspec.

I'm also not clear on what your goal is, per my earlier response.
Please help me understand.

>
> 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
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list