2011-12-08 50 views
1

那麼我已經遍佈整個地方搜索,似乎沒有人有這個'加密'方法導致他們的測試失敗的問題,雖然它似乎很多人有過一些困難第七章沒有進一步再見,RoR教程,第7章 - rspec測試失敗:NoMethodError'encrypt'

下面是用戶模型文件的鏈接哈特爾的Chapter 7

我的代碼,以及相應的規範文件,似乎完全準確的是什麼,他已經寫了,我仍然無法通過測試。錯誤?

Failures: 
1) User should create a new instance given valid attributes 
Failure/Error: User.Create!(@attr) 
NoMethodError: undefined method 'encrypt' for #<User:asdf> 
#./app/models/user.rb:22:in 'has_password?' 
#./app/models/user.rb:28:in 'encrypt_password' 
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>' 

2) User should not allow duplicate email addresses 
Failure/Error: User.Create!(@attr) 
NoMethodError: undefined method 'encrypt' for #<User:asdf> 
#./app/models/user.rb:22:in 'has_password?' 
#./app/models/user.rb:28:in 'encrypt_password' 
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>' 

3) User should reject email addresses identical up to case 
Failure/Error: User.Create!(@attr) 
NoMethodError: undefined method 'encrypt' for #<User:asdf> 
#./app/models/user.rb:22:in 'has_password?' 
#./app/models/user.rb:28:in 'encrypt_password' 
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>' 

... 

7) User has_password? method should be false if passwords do not match 
Failure/Error: User.Create!(@attr) 
NoMethodError: undefined method 'encrypt' for #<User:asdf> 
#./app/models/user.rb:22:in 'has_password?' 
#./app/models/user.rb:28:in 'encrypt_password' 
#./spec/models/user_spec.rb:15:in 'block (3 levels) in <top (required)>' 

所以我得到每個測試相同的錯誤訊息,我要瘋了試圖找出原因!

這裏是我的user.rb:

require 'digest' 
class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

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

    validates :name, :presence => true, 
        :length => { :maximum => 50 } 
    validates :email, :presence => true, 
        :format => { :with => email_regex }, 
        :uniqueness => { :case_sensitive => false } 
#automatically create the virtual attribute for 'password_confirmation' 
    validates :password, :presence => true, 
         :confirmation => true, 
         :length => { :within => 6..40 } 

    before_save :encrypt_password 

    #returns true if the users password matches the submitted one 
    def has_password?(submitted_password) 
    encrypted_password == encrypt(submitted_password) 
    end 

    private 

    def encrypt_password 
    self.salt = make_salt unless has_password?(password) 
    self.encrypted_password = encrypt(password) 
    end 

    def encrypt_string 
    secure_hash("#{salt}--#{string}") 
    end 

    def make_salt 
    secure_hash("#{Time.now.utc}--#{password}") 
    end 

    def secure_hash(string) 
    Digest::SHA2.hexdigest(string) 
    end 
end 

和我user_spec.rb文件:

require 'spec_helper' 
require 'digest' 

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

    it "should create a new instance given valid attributes" do 
User.create!(@attr) 
    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" do 
no_email_user = User.new(@attr.merge(:email => "")) 
no_email_user.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 

    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 not allow duplicate email addresses" do 
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 

    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 password to match the 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 
    long = "a" * 41 
    hash = @attr.merge(:password => long, :password_confirmation => long) 
    User.new(hash).should_not be_valid 
    end 
    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 not allow a blank encrypted password" do 
    @user.encrypted_password.should_not be_blank 
    end 
    end 

    describe "has_password? method" do 

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

    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 
end 

任何幫助將不勝感激。我傾吐了其他的問題,我的代碼,並且改變了各個方面,試圖讓測試起作用,都無濟於事。我希望這不是真的愚蠢,我仍然沒有看到。

回答

3

你的錯誤是在這裏:

def encrypt_string 
    secure_hash("#{salt}--#{string}") 
    end 

要調用下面的ENCRYPT_PASSWORD方法encrypt但你上面的方法被命名爲encrypt_string

def encrypt_password 
    self.salt = make_salt unless has_password?(password) 
    self.encrypted_password = encrypt(password) 
    end 

只是在方法改變encrypt_stringencrypt定義和你應該很好去。

+0

謝謝你,你規則 – user1086832

+1

很高興幫助。如果這回答您的問題,請檢查我的答案旁邊的複選標記以結束此問題 – iwasrobbed

相關問題