Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 methodallow_blank'`

like image 934
Michael Durrant Avatar asked Jul 10 '12 13:07

Michael Durrant


1 Answers

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) }
like image 71
Beat Richartz Avatar answered Oct 20 '22 16:10

Beat Richartz