Michael Durrant Michael Durrant - 2 months ago 11x
Ruby Question

rspec - how to check that allow_blank exists

I have a test for uniqueness which works:

it { should validate_uniqueness_of(:name).case_insensitive }

however when I try to do a similar test for name it fails

it { should validate_presence_of(:name).allow_blank }

I get this error:
undefined method


According to this list, there's no method in shoulda that can be chained to validate_presence_of that would support what you want to do. Have a look at the source code of the matcher here.

However, there is another matcher you can use:

it { should allow_value("", nil).for(:name) }

Which tests if blank values can be applied to your attribute.

The full test would then be

it { should validate_presence_of(:name) }    
it { should allow_value("", nil).for(:name) }