[rspec-users] What's the new syntax for predicates?

David Chelimsky dchelimsky at gmail.com
Wed Mar 14 23:51:29 EDT 2007


On 3/14/07, s.ross <cwdinfo at gmail.com> wrote:
> One of the most compelling things about rSpec is the English-like way
> it reads. I'd vote for anything that keeps it that way. If both can
> "should" and "should be" can be supported it captures things like:
>
> email.to.should include('me at mydomain.com')
>
> and
>
> my_ar_object.should be_valid
>
> Just my $.02.

It happens that #include is supported, but explicitly, not by magic.

The thing that I want to avoid is method_missing causing headaches for
both users and maintainers. With my suggestion earlier this thread,
this could be solved - just not automatic. So you could say

  predicate_matchers :absolutely_anything

and then

  object.should absolutely_anything

and it would pass if

  object.absolutely_anything?

So you'd be able to get the nice natural language feel, but at a small price.

WDYT?

The reason that supporting any arbitrary anything using method_messing
is problematic is that we don't know if the method is missing from the
context or the object being spec'd. So not finding it would produce a
message like:

  RSpec could not find the method #absolutely_anything.
  You need to either define #absolutely_anything in the context
  or #absolutely_anything? on your object.

I don't know about you, but that would really piss me off if I saw
that. I just wrote it and I have no idea what it means :)

Thoughts?

David

>
> Steve
>
>
> On Mar 14, 2007, at 3:48 PM, David Chelimsky wrote:
>
> > On 3/14/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:
> >> On 3/14/07, Pat Maddox <pergesu at gmail.com> wrote:
> >>> On 3/14/07, Pat Maddox <pergesu at gmail.com> wrote:
> >>>> @settings.should allow_publish_ip("127.0.0.1") fails with
> >>>> undefined method `allow_publish_ip' for #<#<Class:0x2f8fd48>:
> >>>> 0x2f5a968>
> >>>>
> >>>> @settings.should_allow_publish_ip("127.0.0.1") works fine
> >>>
> >>> @settings.should_be allow_publish_ip("127.0.0.1") is it
> >>
> >> Unless I'm mistaken (David?) that's deprecated and will disappear. I
> >> believe the way forward is:
> >>
> >> @settings.should be_allow_publish_ip("127.0.0.1")
> >
> > Sad, but true. You need to prefix w/ be_ because don't want to have
> > method_missing doing any more magic than handling methods that start
> > w/ be_ or have_.
> >
> > If you find this unpleasant, you can always provide your own helper
> > methods. Here's a hack that you can use. Maybe we should add this to
> > rspec?
> >
> > require 'rubygems'
> > gem 'rspec'
> > require 'spec'
> >
> > def predicate_matchers(*args)
> >   args.each do |arg|
> >     define_method(arg) do |*args|
> >       send "be_#{arg}", *args
> >     end
> >   end
> > end
> >
> > class Thing
> >   def allow_publish_ip?(ip)
> >     true
> >   end
> > end
> >
> > describe Thing do
> >   predicate_matchers :allow_publish_ip
> >   it "should allow publish ip 123" do
> >     Thing.new.should allow_publish_ip(123)
> >   end
> > end
> >
> >>
> >> Aslak
> >>
> >>> _______________________________________________
> >>> rspec-users mailing list
> >>> rspec-users at rubyforge.org
> >>> http://rubyforge.org/mailman/listinfo/rspec-users
> >>>
> >> _______________________________________________
> >> rspec-users mailing list
> >> rspec-users at rubyforge.org
> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >>
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list