2014-02-16 3 views
0

스택 오버플로가 응답을 찾았을 때 유사한 문제를 게시 한 소수의 사용자를 찾았지만 솔루션이 도움이되지 않았습니다. 바로 내 코드가 정확히 무엇인지 알 수 있습니다.이 사양이 예상대로 작동하지 않는 이유는 무엇입니까?

나는에 자습서를 다음입니다 : 마지막 사양은 실패 http://ruby.railstutorial.org/chapters/modeling-users#code-validates_uniqueness_of_email_case_insensitive_test

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 be_valid } 

    describe "when name is not present" do 
    before { @user.name = " " } 
    it { should_not be_valid } 
    end 

    describe "when email is not present" do 
    before { @user.email = " " } 
    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 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.email = @user.email.upcase 
     user_with_same_email.save 
    end 
    #it "should not be valid" do 
    # expect(@user).not_to be_valid 
    #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_sensative: false } 
end 

나는 완벽하게 튜토리얼을 따라 아직 내가 제대로 작동이 검증을받을 수없는 것. 나는이 튜토리얼이 약간 오래된 것임을 알고 약간의 새로운 보석을 사용하고 있을지도 모른다. 또한이 유형의 유효성 검사를 수행하는 가장 좋은 방법은 아니지만 자습서는 처음에는 간단하게 만드는 것입니다. 여기서 무슨 일이 일어나고있는거야?

감사합니다.


편집 : 는 오류가 없습니다. 유효성 검사가 성공해야 실패 할 수 있습니다. 다시

Failures: 
1) User when email address is already taken should not be valid 
Failure/Error: it { should_not be_valid } 
expected #<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil> not to be valid 
# ./spec/models/user_spec.rb:58:in `block (3 levels in <top (required)>' 

Finished in 0.09887 seconds 
1 example, 1 failure 

편집. user_spec.rb가 완전히 퀘스트에 붙여졌습니다. 미안해. 루비에서 정말 새로운데 문제를 해결하는 데 필요한 것이 무엇인지 알지 못했다.

+1

오류 메시지를 넣을 수 있습니까? –

+0

기대가 충족되지 않거나 제기 된 예외로 인해 실패 했습니까? –

+0

/주제는 어떻게 설정합니까? 즉, 스펙에서 "그것"은 무엇입니까? 문제의 전자 메일은 대문자로되어 있지 않으므로 콜백/등의 소문자를 사용하지 않으면 잘못된 것을 테스트하는 것처럼 보입니다. -b와 함께 실행하고 save!를 호출하십시오. –

답변

2

튜토리얼을 계속 읽는다면 작성자는 테스트가 아직 완료되지 않았다고 말합니다. 현재 유효성 검사가 여전히 대소 문자를 구분하기 때문입니다. uniqueness: { case_sensitive: false } 추가

validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
+0

쏴! 고맙습니다.하지만 우연히 오래된 코드를 붙여 넣었습니다. 나는 그것을 게시 할 때 그것으로 땜질하고 있었다. 현재 코드를 반영하도록 질문을 업데이트했습니다. – Bil1

0

기대로 전달 된 테스트를하게 나는 그냥 그 자체를 해결할 생각합니다. 어쩌면 누군가가 방금 일어난 일을 설명 할 수 있습니다. 튜토리얼을 계속 진행하고 gemfile에 gem 'bcrypt-ruby', '3.1.2'을 추가하기로 결정했습니다. 그런 다음 bundle install을 실행했습니다.

은 그 때 나는 rspec spec/을 실행하는 시도하고 오류 반환 :

Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue. 

을하지만 실행할 때 rake db:migrate 아무 일도하지 않습니다. 그래서 나는 go googled하고 a solution을 가지고 this SO post을 발견했다.

rake test:prepare 
rake db:migrate 

을 다음 모든 테스트를 통과 :

나는 달렸다.

아마도 내 테스트 환경이 나에게 미쳤을 것입니까? 나는 아직도 무슨 일이 일어 났는지 모르겠다.

0

당신은 .save 이전 user_with_same_email.valid?를 추가하고 출력을 제공하지만, 때문에, 그것은 true 것으로 판단 할 수 있습니다 : 당신이 id@user

  • 그냥 변경의 변경되지 않은

    1. 케이스는 이지만 유효하지만 "[email protected]"및 "[email protected]"도 동일하며 유효합니다. :email 이후 true가 존재

    2. #valid 반환, 좋은 Regexp를 가지고 있고, 여전히 독특한.

  • 관련 문제