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

David Chelimsky dchelimsky at gmail.com
Mon May 11 23:21:20 EDT 2009


On Mon, May 11, 2009 at 4:30 PM, Jarmo Pertman <Jarmo.P at gmail.com> wrote:
> Thanks for adding this change into repo!
> I'm quite surprised that anyone else haven't stumbled upon this
> problem yet. I guess it's because most of spec'ing is done for Ruby
> projects, so this functionality is not needed.
>
> I have been thinking a little more about this topic and asked myself:
> why not make it backwards compatible without explicitly stating
> subject as 'self'? If not making it backwards compatible then all my
> current specs which use Watir would have to be changed by adding
> "subject {self}" to them.
>
> Why just not use ExampleGroup as a receiver for #should when string is
> used in ExampleGroup description? I cannot see any negative impacts on
> that change and rake specs are also passing. Any ideas where this
> might fail? There probably aren't any specs where description is
> written as a string and then you want to invoke matchers against that
> string... Doesn't seem logical to me.
>
> So, I've changed locally should method in
> Spec::Example::Subject::ExampleMethods from:
>
>        def should(matcher=nil)
>          self == subject ? self.__should_for_example_group__
> (matcher) : subject.should(matcher)
>        end
>
> to:
>        def should(matcher=nil)
>          if self == subject || (subject.is_a?(String) && subject ==
> instance_eval(&self.class.subject))
>            self.__should_for_example_group__(matcher)
>          else
>            subject.should(matcher)
>          end
>        end
>
> I've added that "&& subject == instance_eval(&self.class.subject)"
> part as an extra precaution, but i don't have any ideas, why it's
> needed anyway, because I cannot think of any cases where string is
> used as a subject... or maybe there are some libraries, which monkey-
> patch String and want to call #should implicitly?! :)
>
> in short, at the moment this seems to work also (rake specs are
> passing again):
>        def should(matcher=nil)
>          if self == subject || subject.is_a?(String)
>            self.__should_for_example_group__(matcher)
>          else
>            subject.should(matcher)
>          end
>        end
>
> If you think that this functionality might be reasonable for wider use
> (some other Watir or similar library users for example), then I can
> provide a patch with specs.

I don't want to promote using self as subject. I think being able to
do so explicitly, as we can now with your previous patch, is perfectly
reasonable. But doing so is a bit of a trick in my view, and runs
counter to the overall intent of the structure of examples of
behaviour of an object or sub-system. "should" is about that object or
sub-system, not self.

If your whole suite is using this, you can localize the "subject
{self}" call, however, in spec_helper, like this:

class Spec::ExampleGroup
  subject {self}
end

HTH,
David


> Regards,
> Jarmo


More information about the rspec-users mailing list