创建多个FactoryGirl记录。验证失败:已收到电子邮件

时间:2015-08-24 15:49:41

标签: ruby-on-rails ruby rspec factory-bot

我正在为Project的索引操作编写RSpec测试。项目属于客户端,属于用户。

我要做的是创建30个用于分页的项目实例。全部属于同一个用户。

当我运行测试时,我发出以下错误(我已尝试rake db test:prepare):

Failure/Error: before(:all)  { @projects = FactoryGirl.create_list(:project, 30, user: user) }
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken

factories.rb

FactoryGirl.define do 
    factory :user do
        name    "John Nana"
        email   "john@nana.com"
        password "woopwoop1"
        password_confirmation "woopwoop1"
    end

    factory :client do
        name "Widget inc."
        user
    end

    factory :user1 do
        name "Bob Inc"
        email "lol@catz.com"
        password "nayy1"
        password_confirmation "nayy1"
    end

    factory :project do
        sequence(:name) { |n| "Project #{n}" }
        fee "250"
        client
    end 
end

project_pages_spec.rb

require 'spec_helper'

describe "create" do

  let(:user) { FactoryGirl.create(:user) }
  let(:submit) { "Create project" }

  describe "when signed in" do

    before do
      capybara_sign_in(user)
      @client = user.clients.create(name: "Widget inc.")
      visit new_project_path
    end 

    describe "with valid information" do

      describe "from /projects" do 
        before do
          visit projects_path
          click_link "Add project"
          fill_in "Project name", with: "Brochure"
          fill_in "Fee", with: "1000"
          select(@client.name, from: 'project_client_id')
        end

        it "should create a project and redirect to /projects" do
          expect { click_button submit }.to change(Project, :count).by(1)
          current_path.should == '/projects'
        end
      end

      before do 
        fill_in "Project name", with: "Brochure"
        fill_in "Fee", with: "1000"
        select(@client.name, from: 'project_client_id')
      end 

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

    describe "with invalid information" do

      before do
        too_long_project = "a" * 50
        fill_in "Project name", with: too_long_project
        fill_in "Fee", with: "1000000000"
        select(@client.name, from: 'project_client_id')
      end

      it "should not create a project" do
        expect { click_button submit }.not_to change(Project, :count)
      end 

    end

  end

  describe "when not signed in" do
    before { visit new_project_path(user) }
    it "should redirect to signin path" do
      current_path.should == '/signin'
    end
  end
end

describe "index" do

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

  before do 
    capybara_sign_in(user)
    visit projects_path
  end

  describe "pagination" do
    before(:all)  { @projects = FactoryGirl.create_list(:project, 30, user: user) }
    after(:all)   { Project.delete_all }

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

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

    it "should list all projects" do 
      @projects.each do |p| 
        expect(page).to have_selector('li', p.name)
      end
    end
  end
end 

1 个答案:

答案 0 :(得分:2)

您可以使用FactoryGirl's Sequences生成唯一的电子邮件地址:

factory :user do
    name    "John Nana"
    sequence(:email) { |n| "person#{n}@example.com" }
    password "woopwoop1"
    password_confirmation "woopwoop1"
end