2014-01-19 58 views
0

我在第6章的rails教程,我運行rspec,它正在下降,並給我這個。Rspec電子郵件測試下降(Rails教程)

Failures: 

    1) User when email address is already taken 
    Failure/Error: user_with_same_email.save 
    NoMethodError: 
     undefined method `save' for "[email protected]":String 
    # ./spec/models/user_spec.rb:55:in `block (3 levels) in <top (required)>' 

Finished in 0.43101 seconds 
21 examples, 1 failure 

Failed examples: 

rspec ./spec/models/user_spec.rb:58 # User when email address is already taken 

而我不明白爲什麼它給了我一個未定義的方法。這是我的測試

測試

require 'spec_helper' 

describe User do 

    before { @user = User.new(name: "Example User", email: "[email protected]") } 

    subject { @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:email) } 
    it { should respond_to(:password_digest) } 

    it { should be_valid } 

    describe "when name is not present" do 
    before { @user.name = " " } 
    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 is not present" do 
    before { @user.email = " " } 
    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 
     expect(@user).not_to 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 
     expect(@user).to be_valid 
     end 
    end 
    end 

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

    it { should_not be_valid } 
    end 
end 

我的用戶模型

class User < ActiveRecord::Base 
    before_save { self.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 } 
end 

,這裏是我的github因爲如果我留下什麼東西了。

回答

1

在此行中的規範:

user_with_same_email = @user.email.upcase 

你把你的用戶對象爲一個字符串(電子郵件地址)。字符串沒有保存方法,因此是錯誤信息。

相關問題