rSpec - added a "either.or.or" Matcher

Hey there guys,
not sure where I should discuss this really,
but I've created a ticket on LightHouse for rSpec.

Came across this problem;

"I have a method which either returns an array or a nil, how can I rspec
assert this?"

solution could be

[NilClass, Array].should include(Model.get_array_or_nil)

but that's rubbish.
I want;

should provide that.
(although it's built against the old svn repo, need to update it for

Any thoughts?



Posted via

I'm not expert of BDD but I think that you should test these 2 cases
separately. I guess that your method doesn't return Array or nil in
random way :). Just split it into two expectations (it "should return
Array when...", it "should return nil when..."). I can't think about
real need to have this functionality (either.or.or) and your example
doesn't show me it.


Radosław Bułat - mój blog

Agreed. You should specify the different conditions under which the
method returns nil or an array in distinct examples. For example,
Array#reject! behaves this way. I'd write some examples like:

describe Array, "#reject!" do
  before(:each) do
    @letters = %w[b c d]

  it "returns nil if no changes were made" do
    @letters.reject! { |letter| letter < 'b' }.should be_nil

  it "returns an array of values remaining after ones matching the
given criteria are rejected" do
   @letters.reject! { |letter| letter > 'b' }.should == ['b']

By the way, the RSpec lists [
] would be a good place to discuss custom matchers.


Craig Demyanovich wrote:

By the way, the RSpec lists [
] would be a good place to discuss custom matchers.

cheers Craig,
I have a feeling I don't do anything in any standard style.

will check out the mailing list,
unfortunately it's not tracked by, which is where I do
all my ruby discussion stuff :frowning:


Posted via\.