2012-03-17 15 views
1

私は初心者からTDDとBDDを使ってアプリケーションを作成しようとしています。RSpecとActiveRecord:無効なシナリオで例が失敗する

モデルの1つでは、フィールドの長さの検証が行われています。この特定のフィールドの長さ検証をチェックする例を持つRSpecがあります。私は、この仕様の例では、次のエラーで失敗し

....F...... 

Failures: 

    1) Section when name is too long 
    Failure/Error: it { should_not be_valid } 
     expected valid? to return false, got true 
    # ./spec/models/section_spec.rb:24:in `block (3 levels) in <top (required)>' 

Finished in 0.17311 seconds 
11 examples, 1 failure 

を鳴らさするとここで

class Section < ActiveRecord::Base 

    # Validations 
    validates_presence_of :name, length: { maximum: 50 } 

end 

とRSpecの

require 'spec_helper' 

describe Section do 
    before do 
     @section = Section.new(name:'Test') 
    end 

    subject { @section } 

    # Check for attribute accessor methods 
    it { should respond_to(:name) } 


    # Sanity check, verifying that the @section object is initially valid 
    it { should be_valid } 

    describe "when name is not present" do 
     before { @section.name = "" } 
     it { should_not be_valid } 
    end 

    describe "when name is too long" do 
     before { @section.name = "a" * 52 } 
     it { should_not be_valid } 
    end 
end 

モデルクラスで、私はここで何かが足りないのですか?

また、RSpec(とShoulda)を使用してモデルをテストする方法については、いくつかの参考文献を提案してください。

答えて

5

validates_presence_ofメソッドにはlengthオプションがありません。

class Section < ActiveRecord::Base 
    # Validations 
    validates_presence_of :name 
    validates_length_of :name, maximum: 50 
end 

それとも新しい検証構文rails3使用します:

​​
+0

おかげでバディを あなたはvalidates_length_of方法で長さを検証する必要があります。エラーが解決しました。私はRails 3.2.2を使用しており、2番目のソリューションを使用しています。 –

関連する問題