2014-04-14 1 views
0

나는 여기 잃어 버렸습니다. Michael Hartl이 만든 Rails 튜토리얼 7 장 중반까지는 모든 것이 순조롭게 진행되었습니다. 나는 전자 메일이 이미 가져 왔다고 말하는 실패 테스트를 받고 있으며 첫 번째 오류로 인해 믿을만한 사용자가 1만큼 카운트를 변경하지 못하게됩니다. 제 6 장 및 본질적으로 코드를 복사하여 내 프로그램에 다시 붙여 넣으면 중첩 오류 또는 유사하지만 그럴 수 없다는 희망이 있습니다. 여기에 실패한 시험은 내가 얻을 것 :이메일 Already Taken, Rails 튜토리얼 chapter 7

Failures: 

1) 사용자 페이지의 프로필 페이지 장애/오류 : (하자 : 사용자) {FactoryGirl.create (: 사용자)} 액티브 :: RecordInvalid가 : 검증 실패 : 이메일 이미 '에서 block (3 levels) in <top (required)>' # ./spec/requests/user_pages_spec.rb:9:in 블록 (3 단계) ./spec/requests/user_pages_spec.rb:8:in #을 촬영되었습니다

페이지는 사용자 장애/오류를 작성해야 유효한 정보로 가입하실 2) 사용자 : {기대 click_button submit} .to change (User, : count) .by (1) 카운트는 1로 변경해야하지만 c

내 사양/모델/user_spec.rb

require 'spec_helper' 

describe User do 

    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar") 
    end 

    subject { @user } 

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

    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]g [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 } 
    end 

    describe "when password is not present" do 
    before do 
     @user = User.new(name: "Example User", email: "[email protected]", 
        password: " ", password_confirmation: " ") 
    end 
    it { should_not be_valid } 
    end 

    describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
    end 

    describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
    end 

    describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should_not be_valid } 
    end 

    describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by(email: @user.email) } 

    describe "with valid password" do 
     it { should eq found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not eq user_for_invalid_password } 
     specify { expect(user_for_invalid_password).to be_false } 
    end 
    end 
end 

user_pages_spec.rb

'블록'0 ./spec/requests/user_pages_spec.rb:43:in # (4 레벨)에 의해 매달아
require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit user_path(user) } 

    it { should have_content(user.name) } 
    it { should have_title(user.name) } 
    end 

    describe "signup page" do 
    before { visit signup_path } 

    it { should have_content('Sign up') } 
    it { should have_title(full_title('Sign up')) } 
    end 

    describe "signup" do 

    before { visit signup_path } 

    let(:submit) { "Create my account" } 

    describe "with invalid information" do 
     it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
     end 
    end 

    describe "with valid information" do 
     before do 
     fill_in "Name",   with: "Example User" 
     fill_in "Email",  with: "[email protected]" 
     fill_in "Password",  with: "foobar" 
     fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
     expect { click_button submit }.to change(User, :count).by(1) 
     end 
    end 
    end 
end 

사람들이 FactoryGirl과 비슷한 문제에 부딪쳤다는 것을 알고 있습니다. 그러나 나중에 내가 수집 한 내용에서 튜토리얼의 후반부에 있으며, 내가 확인한 개발 데이터베이스에 작성하는 것과 관련되어 있습니다. 누구든지 도움, 힌트 또는 자원이 있다면 지금은 크게 감사 할 것입니다.

편집 : 이 User.rb

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 } 
    has_secure_password 
    validates :password, length: { minimum: 6 } 
end 

추가 factories.rb

FactoryGirl.define do 
    factory :user do 
    name  "Example Guy" 
    email "[email protected]" 
    password "foobar" 
    password_confirmation "foobar" 
    end 
end 

감사합니다!

+1

[레일스 팩토리 소녀가 "이메일이 이미 가져갔습니다"] 가능한 복제본 (http://stackoverflow.com/questions/5547730/rails-factory-girl-getting-email-has-already-been-taken) – r00k

답변

1

User 모델과 factories.rb 파일을 보지 않고서는 알기가 어렵지만 사용자의 이메일에 고유성 제약이 있다고 생각합니다. validates :email, uniqueness: true과 같은 것입니다.

또한 사용자 공장에서 하드 코딩 된 이메일 주소를 지정 하셨을 것입니다.

factory_girl은 "[email protected]"이라는 전자 메일을 사용하여 사용자를 생성하고 동일한 전자 메일 주소로 두 번째 사용자를 만들려고하는데 유효성 검사 (올바르게)하면 저장하지 못하게됩니다 이 새로운 사용자. 이 경우

, 당신은 factory_girl으로 생성 된 각 사용자가 독특한 이메일 주소가 당신이 sequences을 사용하여 수행 할 수있는 무언가를 원한다.

+0

원래 게시물을 요청한 파일로 업데이트했습니다. 독창성이 내 쟁점이 아닌 것 같아서 오늘 일찍 시퀀싱을 시도했지만 아무 것도 쓸모가 없습니다. – user3533739

0

동일한 전자 메일을 사용하는 하드 코드 된 사용자가있을 수도 있습니다. 수동으로 사용자를 삭제하거나 bundle exec rake db:reset을 실행하면 문제가되는 사용자를 제거 할 수 있습니다 (이렇게하면 전체 데이터베이스 btw가 삭제되므로 원하는 작업인지 확인하십시오).튜토리얼을 진행하는 동안 몇 번이 문제가 발생했습니다.

관련 문제