2014-01-19 3 views
0

나는 레일 튜토리얼 6 장에 있으며 rspec을 실행 중이며 떨어지고있다.Rspec 전자 메일 테스트가 떨어짐 (레일 튜토리얼)

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 

. 문자열에는 저장 메소드가 없으므로 오류 메시지가 표시됩니다.

관련 문제