Создание нескольких записей FactoryGirl. Проверка не удалась: электронная почта уже была занята
Я пишу тесты RSpec для действия индекса для Project
, Проект принадлежит_ клиенту, который принадлежит_ пользователю.
Я пытаюсь создать 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 ответ
Решение
Вы можете использовать Последовательности FactoryGirl для генерации уникальных адресов электронной почты:
factory :user do
name "John Nana"
sequence(:email) { |n| "person#{n}@example.com" }
password "woopwoop1"
password_confirmation "woopwoop1"
end