Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow #must_be and #wont_be to test truthiness / falsiness #58

Closed
BMorearty opened this issue Oct 11, 2011 · 7 comments
Closed

Allow #must_be and #wont_be to test truthiness / falsiness #58

BMorearty opened this issue Oct 11, 2011 · 7 comments
Assignees

Comments

@BMorearty
Copy link

I appreciate issue #31. Along those lines it would be nice to be able to test truthiness and falsiness without comparing a result to true or false.

Instead of this:

some_obj.truthy_method.must_equal true
some_obj.falsy_method.must_equal false

It would be nice to do this:

some_obj.truthy_method.must_be
some_obj.falsy_method.wont_be

I.e., use MiniTest::Spec to test Ruby's idiomatic Boolean logic where nil and false are falsy and everything else is truthy. I can already do this with MiniTest::Unit:

assert(some_obj.truthy_method)
refute(some_obj.falsy_method)

Note: at first I considered adding Object#truthy? and Object#falsy? methods and using must_be :truthy? etc. But I think the 0-arg must_be and wont_be methods are nicer in two ways: they don't add methods to Object and they are slightly more concise.

(And no, I didn't get this idea from RSpec. :-)

@zenspider
Copy link
Collaborator

I'd rather not overload must_be and wont_be any further. They're complex enough as it is. I'd recommend adding must_be_truthy and wont_be_truthy.

@ghost ghost assigned zenspider Oct 11, 2011
@BMorearty
Copy link
Author

That sounds reasonable.

@zenspider
Copy link
Collaborator

Honestly, I still have misgivings about this issue. The number of plain assert calls that I have in non-rails apps is exactly zero. And the rails apps are rails' fault.

There just isn't much value in saying "this should not be one of two values out of the infinite range of values possible"... Instead, you should test that you actually know what it is. For all you know you get an array back when you think it should be a number... By testing just truthiness, you get very little value (or security) out of your tests.

@BMorearty
Copy link
Author

I can understand that. The case where I find it useful to test truthiness is when a method's documentation states that its return value indicates whether some condition is true, without saying that it specifically returns true or false (i.e. a method whose code doesn't coerce the return value using !!). You said "Instead, you should test that you actually know what it is," but in the case I just mentioned you really don't know what it is--so you can't test against that.

That's the pragmatic argument, and the one that matters to me. The purist argument would be: there should be parity between the test API and the spec API.

@sunaku
Copy link

sunaku commented Nov 8, 2011

Did you know that require "minitest/spec" also pulls in require "minitest/unit"? What prevents you from using MiniTest::Unit's assert() and refute() methods in your specs? It couldn't be because of purity because MiniTest::Spec != RSpec and there's already a divergence in vocabulary ("must" vs. "should") anyway.

@zenspider
Copy link
Collaborator

@sunaku, that's a good point. Assert is available and really... I don't really see the added value of this. Esp if the justification is to test some other library's ill-defined response.

I think I'm going to bury this.

@rickhull
Copy link

rickhull commented Jul 6, 2013

What about just #must? File.exists?('/etc/passwd').must

Kind of weird, in a Louis CK way. But must and wont are sufficient for truthy/falsy. Just throwing it out there.

@minitest minitest locked and limited conversation to collaborators May 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants