2011-06-24 9 views
0

これで完全に困惑しました。私は誓う、これは動作するはずだが、それはdoesntのように見える。テストデータベースが更新されました(rake db:migrate-rake:db:test:clone)。私は何かばかげていることを望んでいる。Rails 3検証質問(Rspec)

私は3.1 RC4を実行しています。

モデル

class ExerciseSet < ActiveRecord::Base 

    TYPES = %w[time rep] 

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

end 

スペック最初の出力

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 

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)>' 

答えて

0

まず物事をファイル - インスタンスでない理由有効ですか?私は、仕様にいくつかのデバッグを追加すると思います:

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 

はい、これはおそらく、コメントする必要がありますが、その後、私はそう簡単にスペックの書き換えを提供していませんでした。

+0

大きな質問です。デバッグのヒントをありがとう。私はそれをどうやってするのだろうと思っていた。私は印刷の声明で遊んで、私が見つけることができるものを見るでしょう。まだこのJavaの世界から出てくるこのルビー/レールを理解しようとしています。 – bballer320xu

+0

パットのプリントステートメントの使用のおかげで、私はこれを理解しました。私はブロックからobject.new文を引っ張り、配列をループするたびにブロックの型フィールドを更新しました。コードは上記のポストに記載されています。私はまだ最初のデザインが失敗した理由について興味があります。 – bballer320xu

+0

モデルに 'attr_accessible'コールがありませんか? – pat