Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating boolean value in Rspec and Rails

I'm pretty confused how to validate boolean values in Rspec and Rails. I understand everything except for false and nil are taken as true in Ruby. But when I use MySQL with Rails, it uses 1 for true and 0 for false (if my understanding is correct).

I have the following model spec. I'd like to test boolean value for superuser attribute.

  • How can I write specs here?
  • How can I write implementation code here?
  • Are my specs and implementation code specific to a specific database (like MySQL and PostgreSQL)?

    require 'spec_helper'
    describe User do
      before(:each) do
        @valid_attributes = {
          :username => "mike",
          :password => "super_encryped_password",
          :email => "[email protected]",
          :superuser => true
        }  
      end
    
      it "should create a new instance given valid attributes" do
        User.create!(@valid_attributes)
      end
    
      it "should have true or false for superuser" do
        @valid_attributes[:superuser] = "hello"
        User.new(@valid_attributes).should have(1).error_on(:superuser)
      end
    end
    
like image 319
TK. Avatar asked Sep 06 '10 01:09

TK.


2 Answers

This is a great discussion with some important points: Rails validates boolean fields automatically, a false value will cause "presence" validations to fail, and a custom validation looking explicitly for true false settings is needed if you want to make sure a boolean field is not nil but has a genuine true or false value.

A quick update for Rails 3.x is the :inclusion helper:

http://guides.rubyonrails.org/active_record_validations_callbacks.html#inclusion

With this helper, validating that a boolean field has a true or a false value looks like this:

validates :superuser, :inclusion => {:in => [true, false], :message => 'requires a true or false value' }

The message is optional and will, as the documentation notes, default to "is not included in the list."

The consensus online seems to be not to bother validating explicitly for true or false, but to let rails take care of it. I ran into a situation, however, where the explicit validations picked up a problem in the database (MySql tinyint fields were set to 2 or 4 in length instead of 1) that caused a strange behavior. The fields could be set to true or false, but the value could not be read back out as a boolean. In other words, the Rails validation passed it through but the explicit inclusion validation flagged all of those cases where the database was misconfigured. So this might be one good reason to go to the trouble of validating those entries. Hope this helps. Charles

like image 84
Charles Forcey Avatar answered Oct 22 '22 04:10

Charles Forcey


One important thing is that ActiveRecord does typecasting behind the scenes, so you do not have to worry about how your database stores boolean values. You even need not validate that a field is boolean, as long as you set that field as boolean when preparing your migration. You may want to make sure the field is not nil though, with a validates_presence_of :superuser declaration in your model class.

like image 20
edgerunner Avatar answered Oct 22 '22 03:10

edgerunner