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

**URL:** <https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599>\
**Category:** Ruby\
**Created:** [May 25, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599 "2008-05-25T17:02:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [May 25, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599/1 "2008-05-25T17:02:09Z")

</div>

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;

Model.get\_array\_or\_nil.should  
either(be\_an\_instance\_of(Array)).or(be\_nil)

[http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/410-adding-an-either-or-matcher](http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/410-adding-an-either-or-matcher)  
should provide that.  
(although it’s built against the old svn repo, need to update it for  
GIT)

Any thoughts?

MatthewRudy

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [May 25, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599/2 "2008-05-25T18:49:56Z")

</div>

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

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [May 25, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599/3 "2008-05-25T19:16:24Z")

</div>

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]  
end

it “returns nil if no changes were made” do  
@letters.reject! { |letter| letter \< ‘b’ }.should be\_nil  
end

it “returns an array of values remaining after ones matching the  
given criteria are rejected” do  
@letters.reject! { |letter| letter \> ‘b’ }.should == [‘b’]  
end  
end

By the way, the RSpec lists [[http://rubyforge.org/mail/?group\_id=797](http://rubyforge.org/mail/?group_id=797)  
] would be a good place to discuss custom matchers.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [May 25, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/rspec-added-a-either-or-or-matcher/138599/4 "2008-05-25T20:41:00Z")

</div>

Craig D. wrote:

> By the way, the RSpec lists [[http://rubyforge.org/mail/?group\_id=797](http://rubyforge.org/mail/?group_id=797)  
> ] 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 [ruby-forum.com](http://ruby-forum.com), which is where I do  
all my ruby discussion stuff ☹
