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

  • Thread starter Matthew Rudy Jacobs
  • Start date
M

Matthew Rudy Jacobs

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
 
R

Rados³aw Bu³at

SSdtIG5vdCBleHBlcnQgb2YgQkREIGJ1dCBJIHRoaW5rIHRoYXQgeW91IHNob3VsZCB0ZXN0IHRo
ZXNlIDIgY2FzZXMKc2VwYXJhdGVseS4gSSBndWVzcyB0aGF0IHlvdXIgbWV0aG9kIGRvZXNuJ3Qg
cmV0dXJuIEFycmF5IG9yIG5pbCBpbgpyYW5kb20gd2F5IDopLiBKdXN0IHNwbGl0IGl0IGludG8g
dHdvIGV4cGVjdGF0aW9ucyAoaXQgInNob3VsZCByZXR1cm4KQXJyYXkgd2hlbi4uLiIsIGl0ICJz
aG91bGQgcmV0dXJuIG5pbCB3aGVuLi4uIikuIEkgY2FuJ3QgdGhpbmsgYWJvdXQKcmVhbCBuZWVk
IHRvIGhhdmUgdGhpcyBmdW5jdGlvbmFsaXR5IChlaXRoZXIub3Iub3IpIGFuZCB5b3VyIGV4YW1w
bGUKZG9lc24ndCBzaG93IG1lIGl0LgoKLS0gClJhZG9zs2F3IEJ1s2F0CgpodHRwOi8vcmFkYXJl
ay5qb2dnZXIucGwgLSBt82ogYmxvZwo=
 
C

Craig Demyanovich

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
 
M

Matthew Rudy Jacobs

Craig said:
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 :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top