[rspec-users] New RSpec methods to Object proposal: should_all and should_none

Paul Hinze paul.t.hinze at gmail.com
Wed Dec 9 15:09:08 EST 2009

Wincent Colaiuta <win at wincent.com> on 2009-12-09 at 13:39:
> El 09/12/2009, a las 19:15, David Chelimsky escribió:
>> On Wed, Dec 9, 2009 at 11:55 AM, Pat Maddox <mailinglists at patmaddox.com 
>> > wrote:
>>> [@admin, @allowed_user].should all(be_allowed_to_visit(url))
>>> [@admin, @allowed_user].should all_be_allowed_to_visit(url)
>>> On Dec 9, 2009, at 5:27 AM, David Chelimsky wrote:
>>> On Wed, Dec 9, 2009 at 5:41 AM, Rodrigo Rosenfeld Rosas
>>> <lbocseg at yahoo.com.br> wrote:
>>>> I was thinking that it would be great to add 2 additional methods to
>>>> Object: should_all and should_none.
>>>> The idea is that we would be able to write tests like:
>>>> [@admin, @allowed_user].should_all be_allowed_to_visit(url)
>>>> [@unprivileged, @non_welcome].should_none be_allowed_to_visit(url)
>>>> Implementation is trivial, but I think that tests would become much
>>>> cleaner than:
>>>> [@admin, @allowed_user].each{|u| u.should be_allowed_to_visit(url)}
>>>> Any thoughts on that?
>>> How about:
>>> each_of(@admin, @allowed_user).should be_allowed_to_visit(url)
>>> none_of(@admin, @allowed_user).should be_allowed_to_visit(url)
>>> This gets the cleanliness without adding to Object.
> I'm puzzled as to why people are so focussed on making specs read like  
> plain text English when they are still developer-facing Ruby code.
> Especially suprised in this case of wanting to avoid the "each + block" 
> enumeration idiom, which is about as "bread and butter" Ruby as you can 
> get, readable to anybody who's ever read the first chapter of a Ruby book.

For me the benefit would be clearer error messages on failures, because
of the potential to continue execution through the entirety of the
collection of inputs, rather than bailing on the first failed 'should'.

This allowing a developer to diagnose classes of problems with the
collection rather than seeing failures one-by-one.

Compare the following two options: 

[:foo, :bar, :baz, :qux].each { |x| x.should ==(:qux) }
  <run 01> "Expected :foo to equal :qux"
           (...fix :foo...)
  <run 02> "Expected :bar to equal :qux"
           (...fix :bar...)
  <run 03> "Expected :baz to equal :qux"
           (...realize the _real_ problem is in dependent code that
            affects all of foo bar and baz, and fix that...)
  <run 04> PASS

each_of(:foo, :bar, :baz, :qux).should ==(:qux)
  <run 01> "Expected each of :foo, :bar, :baz, :qux to equal :qux, but
           :foo, :bar, :baz did not."
           (...fix dependent code that affects all of foo bar and
  <run 02> PASS

I look forward to continuing this discussion.



More information about the rspec-users mailing list