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;

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
should provide that.
(although it’s built against the old svn repo, need to update it for
GIT)

Any thoughts?

MatthewRudy

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 - 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]
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
] would be a good place to discuss custom matchers.

Regards,
Craig

Craig D. wrote:

By the way, the RSpec lists [ 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, which is where I do
all my ruby discussion stuff :frowning: