2012-01-17 22 views
0

bundle exec rspec specを実行すると、21の例と3つの失敗が発生します。これらの障害があること:Hartlチュートリアルの第7章の問題

障害:

1)ユーザーhas_password?パスワードは 失敗/エラーが一致する場合の方法は、真でなければなりません:?user.has_password @(@ attrというの[:パスワード])。: 未定義のメソッド "でhas_password?' for nil:NilClass # ./spec/models/user_spec.rb:47:inブロック(3段階)

2)ユーザー NoMethodErrorをbe_true必要がありますhas_password?パスワードは 失敗/エラーと一致しない場合、メソッドはfalseでなければなりません:?user.has_password @( "無効") NoMethodError be_falseする必要があります。

3)ユーザーズに 未定義のメソッドhas_password?' for nil:NilClass # ./spec/models/user_spec.rb:51:inブロック(3つのレベル)パスワードの検証は、有効な電子メールアドレス 失敗/エラーを受け入れる必要があります。それはやる「無効な電子メールアドレスを拒否しなければならない」 NoMethodError: 未定義のメソッド "でit' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3:0x00000102eb38b0> # ./spec/models/user_spec.rb:97:inブロック(3段階)

私はbcの私のuser_spec.rbファイルを投稿しますそれはほぼ正しいと思うが、完全ではない。コメントアウトされた終わりに注意してください、私は遊びの前にそれらを持っていたが、彼らが間違っていると思ったので、それらをコメントアウトしました。

require 'spec_helper' 

describe User do 

    before(:each) do 
    @attr = { 
     :name => "Example User", 
     :email => "[email protected]", 
     :password => "foobar", 
     :password_confirmation => "foobar" } 
    end 

    it "should create a new instance given valid attributes" do 
    User.create!(@attr) 
    end 

    describe "password encryption" do 

    before(:each) do 
     @user = User.create!(@attr) 
    end 

    it "should have an encrypted password attribute" do 
     @user.should respond_to(:encrypted_password) 
    end 

    it "should set the encrypted password" do 
     @user.encrypted_password.should_not be_blank 
    end 
end 

    describe "has_password? method" do 

    it "should be true if the passwords match" do 
     @user.has_password?(@attr[:password]).should be_true 
    end 

    it "should be false if the passwords don't match" do 
     @user.has_password?("invalid").should be_false 
    end 
end 

    describe "password validations" do 

    it "should require a password" do 
    User.new(@attr.merge(:password => "", :password_confirmation => "")). 
     should_not be_valid 
    end 

    it "should require a matching password confirmation" do 
    User.new(@attr.merge(:password_confirmation => "invalid")). 
     should_not be_valid 
    end 

    it "should reject short passwords" do 
    short = "a" * 5 
    hash = @attr.merge(:password => short, :password_confirmation => short) 
    User.new(hash).should_not be_valid 
    end 

    it "should reject long passwords" do 
    short = "a" * 5 
    hash = @attr.merge(:password => short, :password_confirmation => short) 
    User.new(hash).should_not be_valid 
    end 

    it "should require a name" do 
    no_name_user = User.new(@attr.merge(:name => "")) 
    no_name_user.should_not be_valid 
    end 

    it "should require an email address" do 
    no_email_user = User.new(@attr.merge(:email => "")) 
    no_email_user.should_not be_valid 
    end 

    it "should accept valid email addresses" do 
    addresses = %w[[email protected] [email protected] [email protected]] 
    addresses.each do |address| 
     valid_email_user = User.new(@attr.merge(:email => address)) 
     valid_email_user.should be_valid 
    end 
    #end 

    it "should reject invalid email addresses" do 
    addresses = %w[[email protected],com user_at_foo.org [email protected]] 
    addresses.each do |address| 
     invalid_email_user = User.new(@attr.merge(:email => address)) 
     invalid_email_user.should_not be_valid 
    end 
    #end 

    it "should reject duplicate email addresses" do 
    # Put a user with given email address into the database. 
    User.create!(@attr) 
    user_with_duplicate_email = User.new(@attr) 
    user_with_duplicate_email.should_not be_valid 
    end 

    it "should reject email addresses identical up to case" do 
     upcased_email = @attr[:email].upcase 
     User.create!(@attr.merge(:email => upcased_email)) 
     user_with_duplicate_email = User.new(@attr) 
     user_with_duplicate_email.should_not be_valid 
    end 

    it "should reject names that are too long" do 
    long_name = "a" * 51 
    long_name_user = User.new(@attr.merge(:name => long_name)) 
    long_name_user.should_not be_valid 
     end 
     end 
    end 
    end 
end 

私のuser.rbファイルはいいと思います。

だから、3つの障害の事は私の問題の一つの側面ですが、本当に私を心配事は、次のコマンドです:

バンドルのexec RSpecの仕様/モデル/ user_spec.rb -e「has_password \方法?」

いいえ例一致{:(? - ミックス:has_password \\ \方法?)full_description =>//}端末における

結果はこれです。

は、私が2例、および0の障害を持っている必要がありHARTLによる0.00003秒 0例、0失敗

に仕上がっています。アイデア?任意の入力はあなたのuser_spec.rbファイルの

答えて

0

にあります。

は、あなたが持っていることを確認してください。

 before(:each) do 
     @user = User.create!(@attr) 
     end 

右の次の行の後:それはチュートリアルのコードから欠落しています

 describe "has_password? method" do 

。それはパスワード暗号化ブロックの一部であることがわかります。そのテストのためにユーザをスタブしているように見えます。それはあまりDRYではないでしょう...おそらく、そのスタブブロックをそれぞれの記述ブロックに対して実行させる方法ですが、それは私よりもずっと進んでいます。 :)それが助けてくれることを願って。

関連する問題