开发者

Rails 3 Validation Question (Rspec)

Completely stumped by this. I swear, this looks like it should work bu开发者_开发技巧t it doesnt. The testing database has been updated (rake db:migrate - rake:db:test:clone). I'm hoping I'm doing something silly.

Full disclosure, I'm running the 3.1 RC4.

Model

class ExerciseSet < ActiveRecord::Base

  TYPES = %w[time rep]

  validates :type,
            :presence => true,
            :inclusion => { :in => TYPES }

end

Spec File

require 'spec_helper'

describe ExerciseSet do

  before(:each) do
    @attr = { :value => 12, 
              :value_max => 15,
              :type => "timed",
              :target => "range"
    }            
  end


  describe "type" do
    it "must be either 'time' or 'rep'" do
      values = %w[time rep]
      values.each do |value|
        exercise_instance = ExerciseSet.new(@attr.merge(:type  => value))
        exercise_instance.should be_valid
      end
    end
  end

end

Output

Failures:

  1) ExerciseSet type must be either 'time' or 'rep'
     Failure/Error: exercise_instance.should be_valid
       expected valid? to return true, got false
     # ./spec/models/exercise_set_spec.rb:19:in `block (4 levels) in <top (required)>'
     # ./spec/models/exercise_set_spec.rb:17:in `each'
     # ./spec/models/exercise_set_spec.rb:17:in `block (3 levels) in <top (required)>'


First things first - why isn't the instance valid? I'd be adding some debugging to the spec:

describe "type" do
  it "must be either 'time' or 'rep'" do
    values = %w[time rep]
    values.each do |value|
      exercise_instance = ExerciseSet.new(@attr.merge(:type  => value))
      exercise_instance.valid? # force errors
      puts exercise_instance.errors.full_messages.join("\n")
      exercise_instance.should be_valid
    end
  end
end

Yes, this should probably be a comment, but then I couldn't offer the spec rewrite so easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜