[rspec-users] Can I do foo.should (be_nil || be_empty) in rspec

David Chelimsky dchelimsky at gmail.com
Tue Nov 4 12:07:20 EST 2008


On Tue, Nov 4, 2008 at 9:29 AM, Pat Maddox <pergesu at gmail.com> wrote:
> "Andrew Premdas" <apremdas at gmail.com> writes:
>
>> General case I'm thinking about is just testing that something should be one thing or another
>>
>>    e.g x.should be(foo || bar)
>>
>> Haven't got a specific example at the moment apart from the blank one.  What I'm thinking about is the syntax to say this in a spec, as I
>> think there must be lots of times when having this sort of syntax would be useful.
>
> Hi,
>
> This is a case where a custom matcher would be hiding a concept that
> belongs directly in your code.  If you really don't care about the
> difference between empty and nil, you should create a method - such as
> #blank? - that handles that for you.

In which case you would get be_blank for free, in case you weren't aware.

FYI - if you REALLY want to match against either, it's pretty easy to
do w/ simple matcher:

def be_nil_or_empty
  simple_matcher "nil? or empty? to return true" do |actual|
    actual.nil? || actual.empty?
  end
end

Cheers,
David

>
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list