Topic: Rspec and boolean type in Model Spec

This is probably not an Rspec issue but I probably don't understand something.  I am a Rails and Rspec Noob.

schema.rb

  create_table "users", :force => true do |t|
    t.string   "login"
    t.string   "hashed_password"
    t.string   "email"
    t.string   "salt"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "password_is_temp"
  end

user.rb
class User < ActiveRecord::Base
 
  validates_presence_of :login
  validates_presence_of :hashed_password
  validates_presence_of :email
  validates_presence_of :password_is_temp
 
end

user_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'

module UserSpecHelper
 
  def valid_user_attributes
    {
      :login => "aaaaaa",
      :hashed_password => "aaaaaa",
      :password_is_temp  => true,
      :email => "me@myself.com",
    }
  end
 
end
# Verify Model Structure
describe User do
 
  include UserSpecHelper
 
  before(:each) do
    @user = User.new
  end

  it "should be valid" do
    @user.attributes = valid_user_attributes
    @user.should be_valid
  end
  ....


The issue is when I set :password_is_temp  => true in the UserSpecHelper the example passes but when I set :password_is_temp  => false the example fails.

Thank you for your assistance

Barb #cool#

Re: Rspec and boolean type in Model Spec

class User < ActiveRecord::Base
  # Don't do this
  # validates_presence_of :password_is_temp

  # Do this
  validates_inclusion_of :password_is_temp, :in => [true, false]
end


http://api.rubyonrails.org/classes/Acti … ml#M001327
"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 due to the way Object#blank? handles boolean values. false.blank? # => true"

Vincent Woo Ruby on Rails Blog

Re: Rspec and boolean type in Model Spec

vwoo,

THANK YOU!!! #big_smile# I knew it was an operator error!!

Barb #cool#