2012-12-25 3 views
0

나는 무언가를 망가뜨 렸습니다. 있는 Hartl의 레일 튜토리얼의 섹션 9.3.2 후, 나는 갑자기 27 오류를 유사 모든 말을 뭔가 얻을 : 내 이전의 낙하를왔다 오타 나 잘못된 ''에 대해 검토 한 결과"유효성 검사가 실패했습니다 : 이메일이 유효하지 않습니다"Hartl Rails Tutorial

23) Authentication signin with valid information 
Failure/Error: let(:user) { FactoryGirl.create(:user) } 
ActiveRecord::RecordInvalid: 
    Validation failed: Email is invalid 
# ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels) in <top (required)>' 
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>' 

,하지만하지 않은 보았다. 다음은 관련 페이지입니다.

factories.rb

FactoryGirl.define do 
    factory :user do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}" } 
    password "foobar" 
    password_confirmation "foobar" 
    end 
end 

lib 디렉토리/작업/sample_data.rake

namespace :db do 
    desc "fill database with sample data" 
    task populate: :environment do 
    User.create!(name: "Example User", 
       email: "[email protected]", 
       password: "foobar", 
       password_confirmation: "foobar") 
    99.times do |n| 
     name = Faker::Name.name 
     email = "example-#{n+1}@railstutorial.org" 
     password = "password" 
     User.create!(name: name, 
       email: email, 
       password: password, 
       password_confirmation: password) 
    end 
    end 
end 

user_pages_spec.rb

class UsersController < ApplicationController 
    before_filter :signed_in_user, only: [:index, :edit, :update] 
    before_filter :correct_user, only: [:edit, :update] 

    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    private 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to signin_url, notice: "Please sign in." 
     end 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 
end 

user_pages_spec.rb

require 'spec_helper' 

describe "User Pages" do 

    subject { page } 

    describe "index" do 

    let(:user) { FactoryGirl.create(:user) } 

    before(:each) do 
     sign_in user 
     visit users_path 
    end 

    it { should have_selector('title', text: 'All users') } 
    it { should have_selector('h1', text: 'All users') } 

    describe "pagination" do 

     before(:all) { 30.times { FactoryGirl.create(:user) } } 
     after(:all) { User.delete_all } 

     it { should have_selector('div.pagination') } 

     it "should list each user" do 
     User.paginate(page: 1).each do |user| 
      page.should have_selector('li', text: user.name) 
     end 
     end 
    end 
    end 

    describe "signup page" do 
    before { visit signup_path } 

    it { should have_selector('h1', text: 'Sign up') } 
    it { should have_selector('title', text: full_title('')) } 
    end 

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

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 
    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 

     describe "after submission it should" do 
     before { click_button submit } 

     it { should have_selector('title', text: 'Sign Up') } 
     it { should have_content('error') } 
     end 
    end 

    describe "with valid information" do 
     before { valid_signup } 

     it "should create a user" do 
     expect { click_button submit }.to change(User, :count).by(1) 
     end 
     describe "after saving the user" do 
     before { click_button submit } 
     let(:user) { User.find_by_email('[email protected]') } 

     it { should have_selector('title', text: user.name) } 
     it { should have_welcome_message('Welcome') } 
     it { should have_link('Sign out') } 
     end 
    end 
    end 

    describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
     sign_in user 
     visit edit_user_path(user) 
    end 

    describe "page" do 
     it { should have_selector('h1', text: "Update your profile") } 
     it { should have_selector('title', text: "Edit user") } 
     it { should have_link('change', href: 'http://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
     before { click_button "Save changes" } 

     it { should have_content('error') } 
    end 

    describe "with valid information" do 
     let(:new_name) { "New Name" } 
     let(:new_email) { "[email protected]" } 

     before do 
     fill_in "Name",    with: new_name 
     fill_in "Email",   with: new_email 
     fill_in "Password",   with: user.password 
     fill_in "Confirm Password", with: user.password 
     click_button "Save changes" 
     end 

     it { should have_selector('title', text: new_name) } 
     it { should have_selector('div.alert.alert-success') } 
     it { should have_link('Sign out', href: signout_path) } 
     specify { user.reload.name.should == new_name } 
     specify { user.reload.email.should == new_email } 
    end 
    end 
end 
012 3,516,

당신은 잘못 공장

FactoryGirl.define do 
    factory :user do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}" } 
    password "foobar" 
    password_confirmation "foobar" 
    end 
end 

:email을 정의한

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 

    describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1', text: 'Sign in') } 
    it { should have_selector('title', text: 'Sign in') } 
    end 

    describe "signin" do 
    before { visit signin_path } 

    describe "with invalid information" do 
     before { click_button "Sign in" } 

     it { should have_selector('title', text: 'Sign in') } 
     it { should have_error_message('Invalid') } 
    end 

     describe "after visiting another page" do 

     before { click_link "Home" } 
     it { should_not have_error_message('Invalid') } 
     end 

    describe "with valid information" do 
     let(:user) { FactoryGirl.create(:user) } 
     before { sign_in user } 

     it { should have_selector('title', text: user.name) } 
     it { should have_link('Users',  href: users_path) } 
     it { should have_link('Profile',  href: user_path(user)) } 
     it { should have_link('Settings', href: edit_user_path(user)) } 
     it { should have_link('Sign out', href: signout_path) } 
     it { should_not have_link('Sign in', href: signin_path) } 

     describe "followed by signout" do 
     before { click_link "Sign out" } 
     it { should have_link('Sign In') } 
     end 
    end 
    end 

    describe "authorization" do 

    describe "for non-signed-in users" do 
     let(:user) { FactoryGirl.create(:user) } 

     describe "when attempting to visit a protected page" do 
      before do 
      visit edit_user_path(user) 
      fill_in "Email",  with: user.email 
      fill_in "Password", with: user.password 
      click_button "Sign in" 
      end 

     describe "after signing in" do 

      it "should render the desired protected page" do 
      page.should have_selector('title', text: 'Edit user') 
      end 
     end 
     end 

     describe "in the Users controller" do 

     describe "visiting the edit page" do 
      before { visit edit_user_path(user) } 
      it { should have_selector('title', text: 'Sign in') } 
     end 

     describe "visiting the user index" do 
      before { visit users_path } 
      it { should have_selector('title', text: 'Sign in') } 
     end 

     describe "submitting to the update action" do 
      before { put user_path(user) } 
      specify { response.should redirect_to(signin_path) } 
     end 
     end 
    end 

    describe "as wrong user" do 
     let(:user) { FactoryGirl.create(:user) } 
     let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") } 
     before { sign_in user } 

     describe "visiting Users#edit page" do 
     before { visit edit_user_path(wrong_user) } 
     it { should_not have_selector('title', text: full_title('Edit user')) } 
     end 

     describe "submitting a PUT request to the Users#update action" do 
     before { put user_path(wrong_user) } 
     specify { response.should redirect_to(root_path) } 
     end 
    end 
    end 
end 
+0

모델의 검증은 무엇이어야 하는가? – apneadiving

답변

2

authentication_pages_spec.rb는

sequence(:email) { |n| "person_#{n}@example.com" } 
+0

감사합니다. 나는 Hartl의 경로에서 약간 벗어났다. (domain @ railstutorials.org와 이메일 주소를 바꾼다.) 데이터베이스를 재설정하는 것에 익숙해졌다. (나는 이메일을 만들어서 이메일에 중복 이메일을 만들었다 고 말했기 때문에) 여하튼. –

관련 문제