79801

rspec - how to check that allow_blank exists

Question:

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

Answer1:

According to <a href="https://web.archive.org/web/20150904101414/http://cheat.errtheblog.com/s/rspec_shoulda" rel="nofollow">this list</a>, 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 <a href="https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb" rel="nofollow">the source code of the matcher here</a>.

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) }

Answer2:

AFAIK, there is no matcher for allow_blank so you could use allow_value(@subject.name.blank?) or something similar.

As a side note, I never test model validation using the validation methods themselves as you're actually testing the wrong thing. I test them using creation of the model in question, passing it valid or invalid parameters. This will test the model much more accurately and in real-world conditions.

Recommend

  • Rails / Rspec - writing spec for class name of belongs_to association
  • I try to sort an array using sort() but it fails
  • How to return an object in my case?
  • copy an attribute of a property from one instance to another instance (of a different type ) at runt
  • Cannot use a lambda expression as an argument to a dynamically dispatched operation
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Rails Find when some params will be blank
  • Hardware Accelerated Image Scaling in windows using C++
  • Asynchronous UI Testing in Xcode With Swift
  • Counter field in MS Access, how to generate?
  • angularjs unit test when to use $rootScope.$new()
  • Can I make an Android app that runs a web view in Chrome 39?
  • Trying to switch camera back to front but getting exception
  • Javascript + PHP Encryption with pidCrypt
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Websockets service method fails during R startup
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • How to include full .NET prerequisite for Wix Burn installer
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Turn off referential integrity in Derby? is it possible?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • Add sale price programmatically to product variations
  • Programmatically clearing map cache
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?