2012-09-24 90 views
0

我與Rails tutoril戰鬥。我已經在第六章中討論過了。這是問題所在 當我運行測試:Rails 3教程第6章奇怪的Rspec失敗

bundle exec rspec spec/models/user_spec.rb 

I get few failures: 

Failures: 

1) User 
←[31mFailure/Error:←[0m ←[31mit { should be_valid }←[0m 
←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:31:in `block (2 levels) in <top (required 
)>'←[0m 

2) User when email format is valid should be valid 
←[31mFailure/Error:←[0m ←[[email protected] be_valid←[0m 
    ←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:64:in `block (4 levels) in <top (required 
)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:62:in `each'←[0m 
←[36m  # ./spec/models/user_spec.rb:62:in `block (3 levels) in <top (required 
)>'←[0m 

3) User return value of authenticate method with valid password 
←[31mFailure/Error:←[0m ←[31mit { should == found_user.authenticate(@user.p 
assword) }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:99:in `block (4 levels) in <top (required 
)>'←[0m 

4) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
authenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:103:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:105:in `block (4 levels) in <top (require 
d)>'←[0m 

5) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:103:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:106:in `block (4 levels) in <top (require 
d)>'←[0m 

6) User return value of authenticate method with valid password 
←[31mFailure/Error:←[0m ←[31mit { should == found_user.authenticate(@user.e 
mail) }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:120:in `block (4 levels) in <top (require 
d)>'←[0m 

7) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:124:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:126:in `block (4 levels) in <top (require 
d)>'←[0m 

8) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:124:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:127:in `block (4 levels) in <top (require 
d)>'←[0m 

9) User with a password that's too short 
←[31mFailure/Error:←[0m ←[31mit { should be_valid }←[0m 
    ←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:112:in `block (3 levels) in <top (require 
d)>'←[0m 

Finished in 8.5 seconds 
←[31m23 examples, 9 failures←[0m 

Failed examples: 

←[31mrspec ./spec/models/user_spec.rb:31←[0m ←[36m# User ←[0m 
←[31mrspec ./spec/models/user_spec.rb:60←[0m ←[36m# User when email format is va 
lid should be valid←[0m 
←[31mrspec ./spec/models/user_spec.rb:99←[0m ←[36m# User return value of authent 
icate method with valid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:105←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:106←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:120←[0m ←[36m# User return value of authen 
ticate method with valid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:126←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:127←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:112←[0m ←[36m# User with a password that's 
too short ←[0m 

我User_spec.rb

require 'spec_helper' 

describe User do 

before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", 
    password_confirmation: "foobar") 
end 

subject { @user } 

it { should respond_to(:name) } 
it { should respond_to(:email) } 
it { should respond_to(:password_digest) } 
it { should respond_to(:password) } 
it { should respond_to(:password_confirmation) } 
it { should respond_to(:authenticate) } 

it { should be_valid } 

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

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

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

describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] 
         [email protected]_baz.com [email protected]+baz.com] 
     addresses.each do |invalid_address| 
      @user.email = invalid_address 
      @user.should_not be_valid 
     end 
    end 
end 

describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] 
[email protected]] 
     addresses.each do |valid_address| 
      @user.email = valid_address 
      @user.should be_valid 
     end 
    end 
end 

describe "when email addresses is already taken" do 
    before do 
     user_with_same_email = @user.dup 
     user_with_same_email.email = @user.email.upcase 
     user_with_same_email.save 
    end 

    it { should_not be_valid } 
end 

describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " "} 
    it { should_not be_valid } 
end 

describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
end 

describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) } 

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_valid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) }  

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.email) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 
end 

My user.rb model: 

    class User < ActiveRecord::Base 
    attr_accessible :email, :name, :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.email = email.downcase } 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: 
    { case_sensitive: false } 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    end 

請誰能幫助!我已經在這個問題上停了兩週左右,但仍然不知道該怎麼做。

+0

安置自己的用戶模型 –

回答

0

我覺得這是你的VALID_EMAIL_REGEX區域

你已經有了:

VALID_EMAIL_REGEX = /\A[w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

Github上有:從評論

VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

https://github.com/railstutorial/sample_app_2nd_ed/blob/master/app/models/user.rb

編輯 - 如果我是你我會複製github或at的相關位最少與具有錯誤的人進行比較。

從GitHub:

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_invalid } 
end 

從您的應用程序

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_valid } 
end 
+0

我認爲這是正確的方向。現在我們只得到兩個失敗:1)用戶密碼太短 ←[31m失敗/錯誤:←[0m←[31mit {should be_valid}←[0m ←[31mexpected valid?返回true,得到false←[0m ←[36m#./spec/models/user_spec.rb:112:in'block(3 levels)in '←[0m – szatan

+0

2)User返回有效密碼的驗證方法的值 ←[31m失敗/錯誤:←[0m←[31mit {should == found_user.authenticate(@ user.e mail)}←[0m ←[31mexpected:false←[0m ←[31m got:#<用戶名:1​​,名稱:「示例用戶」,電子郵件:「[email protected] om」,created_at:「2012-09-24 19:46:31」,updated_at:「2012 -09-24 19:46:31「,passw ord_digest:」$ 2a $ 10 $ alTP8uWKt7mzDUw/wEsVo.ERM7Q3ivRlQvi0zX5xY5Oo ...「>(使用==) ←[0m ←[36m#./spec/models/ user_spec.rb:120:在'←[0米 – szatan

+0

失敗的例子: ←[用密碼這是 太短31mrspec./spec/models/user_spec.rb:112←[0m←[36米#用戶←[0米 ←[31mrspec ./spec/models/user_spec.rb:120←[0m←[36m#認證用戶返回值 帶有效密碼的ticate方法←[0m – szatan

0

另外檢查是否已正確初始化的@user,在6月底的一章, 在user_spec.rb,初始化應該看起來像,

之前做

@user = User.new(name: "Example User", email: "[email protected]",password: "foobar", password_confirmation: "foobar") 
    end 

注意,所有必需的用戶屬性應傳遞(在這種情況下,姓名,電子郵件,密碼和password_confirmation)

相關問題