0

brief.rb

# encoding: utf-8
class Brief < ActiveRecord::Base
  belongs_to :project  
  validates_numericality_of :duration, :only_integer => true, :less_than_or_equal_to => 15, :greater_than_or_equal_to => 5, :unless => Proc.new{ |b| b.project.project_type.express_project }
end

brief_spec.rb

require 'spec_helper'

describe Brief do

  before(:all) do
    @project =Factory(:project)
    @brief=Factory.build(:brief,:project => @project)
  end

  context 'non-specific tests' do

    subject { @brief }

    it { should belong_to(:project) }

    it { should validate_presence_of(:project}

  end
end

These are my brief model and brief spec file.But I didn't figure out how can I test validates_numericality_of unless part.Can anyone tried it before?

Johnny Cash
  • 4,867
  • 5
  • 21
  • 37

1 Answers1

1

use context and test different conditions

e.g.

context "when duration is more than 5 but less than 15" do 
  before do 
    subject.duration = 10
  end
  it { should validate_presence_of(:project) }
end
context "when duration is more than 15" do 
  before do 
    subject.duration = 20
  end
  it { should_not validate_presence_of(:project) }
end
John Hinnegan
  • 5,864
  • 2
  • 48
  • 64