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
빈 문자열이 빈 문자열과 다른 경우, 맞습니까? 그래서 이메일 = ""} 그냥 빈 상자가 아니라 전자 메일이 비어 있는지 확인하십시오? 나는 말이 되니? haha – TheBetterJORT
공간이 여전히 문자 인 것처럼 나는 빈 것과 빈을 같은 의미로 사용합니다. 당신의 예제에서 문자열은''''- 공간을 적어 둡니다. 레일스에 따르면, 그것은 비어 있지 않은 문자열이고 비어 있지도 않습니다. –
설명 : 레일에서'' ".blank? == ""비어 있습니다. == true',하지만'' ".blank? == 참; 비어 있습니다. == false' !! 즉, 공백을 포함하는 문자열 **은 비어 있지만 ** 비어 있지 않습니다 **. –