0

简介.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

简要规范.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

这些是我的简要模型和简要规范文件。但我不知道如何测试 validates_numericality_of除非部分。有人以前试过吗?

4

1 回答 1

1

使用上下文并测试不同的条件

例如

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
于 2013-04-04T19:16:34.697 回答