Shoulda-matchers: BOOLEAN_ALLOWS_BOOLEAN_MESSAGE warning is invalid

Created on 21 Jul 2015  Â·  6Comments  Â·  Source: thoughtbot/shoulda-matchers

I have a custom ActiveModel::Model in which I'm validating true/false inclusion:

# model
validates :is_org, inclusion: { in: [true, false] }

# spec
it { is_expected.to validate_inclusion_of(:is_org).in_array([true, false]) }

This results in the BOOLEAN_ALLOWS_BOOLEAN_MESSAGE warning being output during my tests run:


Warning from shoulda-matchers:

You are using validate_inclusion_of to assert that a boolean column
allows boolean values and disallows non-boolean ones. Be aware that it
is not possible to fully test this, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you

should consider removing this test.

The problem with this is that the warning is only valid for ActiveRecord, Mongoid, or other ORMs that translate values to true or false. If just using an ActiveModel or other bare-bones model, this is not true.

This can be demonstrated by simply adding an allow_value matcher to the suite:

# spec
it { is_expected.to allow_value('true').for(:is_org) }

# output
Failure/Error: it { is_expected.to allow_value('true').for(:is_org) }
  Did not expect errors when is_org is set to "true",
  got errors:
  * "is not included in the list" (attribute: is_org, value: "true")

This warning should go away, or it should have an option to disable it since it's incorrectly mucking up my test output.

UX PR Welcome

Most helpful comment

You're right, and I didn't think about that.

I'm not sure when I'll be able to get to this, but I'll leave it open until then.

All 6 comments

You're right, and I didn't think about that.

I'm not sure when I'll be able to get to this, but I'll leave it open until then.

I don't mind finding the time to get a PR together as well, happy to help!
On Jul 21, 2015 5:38 PM, "Elliot Winkler" [email protected] wrote:

You're right, and I didn't think about that.

I'm not sure when I'll be able to get to this, but I'll leave it open
until then.

—
Reply to this email directly or view it on GitHub
https://github.com/thoughtbot/shoulda-matchers/issues/761#issuecomment-123509448
.

+1

+1

I think we should probably remove this warning in the following cases:

  • If the model is not an ActiveRecord model
  • If the model is an ActiveRecord model, but the column is nullable

The Rails documentation has this to say under validates_presence_of:

If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [true, false].

This is technically true, but it will have no effect if either of the conditions above are false.

Hey folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the shoulda-matchers team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue.

Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings