2013-10-15 3 views
0

Michael Hartl의 Ruby on Rails 튜토리얼의 chapter 6.x에서 Rspec이 email is not present 수표를 통과 할 수 없습니다.RailsTutorial.org 6 장 Michael Hartl Rspec 빈 이메일 확인

내 제한된 지식에서

:

  • User_spec 이전 할 후 코드를 사용하여 테스트 사용자를 만듭니다.
  • 이 사용자는 user.rb의 특성과 비교하여 이 :presence이 유효하다는 것을 확인합니다.
  • 그런 다음 검사는 해당 유효성이 true 또는 false 인 경우 true 또는 false를 반환합니다. 다음 코드에서
  • 는 전에 {@ user.email = "는"} 다음 it { should_not be_valid }

그러나, 그것은 User when email is not present 오류를 실패라고 빈

  • 에 이메일을 설정합니다.

    /spec/models/User_spec.rb

    require 'spec_helper' 
    
    describe User do 
    
        before do 
        @user = User.new(name: "Example User", email: "[email protected]") 
        end 
    
        subject { @user } 
    
        it { should respond_to(:name) } 
        it { should respond_to(:email) } 
    
        it { should be_valid } 
    
        describe "when email is not present" do 
        before { @user.email = " " } 
        it { should_not be_valid } 
        end 
    end 
    

    spec/models/user.rb

    class User < ActiveRecord::Base 
        validates :name, presence: true 
        validates :email, presence: true 
    end 
    

    실패 :

    1) User when email is not present Failure/Error: it { should_not be_valid } expected # not to be valid # ./spec/models/user_spec.rb:18:in `block (3 levels) in '

    Finished in 0.03278 seconds 
    4 examples, 1 failure 
    
    Failed examples: 
    
    rspec ./spec/models/user_spec.rb:18 # User when email is not present 
    
  • 답변

    2

    당신은 값이 전무 인 경우는 실패 할 것이라는 점을 의미하는 존재로 이메일을 확인하는 것 또는 빈 문자열.

    전자 메일이 없어야하는 경우를 설명하는 rspec 블록에서 전자 메일 주소를 단일 공백으로 구성된 문자열로 정의합니다. 공백을 제거하여 빈 문자열을 만들고 싶습니다.

    before { @user.email = "" } 
    

    이제 유효성 검사가 실패합니다.

    +0

    빈 문자열이 빈 문자열과 다른 경우, 맞습니까? 그래서 이메일 = ""} 그냥 빈 상자가 아니라 전자 메일이 비어 있는지 확인하십시오? 나는 말이 되니? haha – TheBetterJORT

    +0

    공간이 여전히 문자 인 것처럼 나는 빈 것과 빈을 같은 의미로 사용합니다. 당신의 예제에서 문자열은''''- 공간을 적어 둡니다. 레일스에 따르면, 그것은 비어 있지 않은 문자열이고 비어 있지도 않습니다. –

    +0

    설명 : 레일에서'' ".blank? == ""비어 있습니다. == true',하지만'' ".blank? == 참; 비어 있습니다. == false' !! 즉, 공백을 포함하는 문자열 **은 비어 있지만 ** 비어 있지 않습니다 **. –

    관련 문제