【发布时间】:2014-02-28 19:14:10
【问题描述】:
使用下面的工厂时出现错误(如下所示)。它通过运行rake db:test:prepare 临时修复,但是,这只修复了一次测试运行。我能做些什么来获得更永久的修复?我应该使用database_cleaner 吗?如果是这样,任何人都可以提供任何有关设置它的见解(我正在使用 RSpec、Capybara、Rails 4、Ruby 2)?谢谢!
我正在使用以下工厂:
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com" }
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :key do
sequence(:name) { |n| "Key #{n}" }
sequence(:description) { |n| "This is key #{n}" }
root_note "C"
accidental "Natural"
third "Major"
user
end
factory :chord do
root_note "C"
accidental "Natural"
third "Major"
fifth ""
extension ""
sequence(:name) { |n| "Chord #{n}" }
sequence(:description) { |n| "This is chord #{n}" }
user
factory :submitted_chord do
state 'Submitted'
end
factory :published_chord do
state 'Published'
end
end
end
使用工厂时出现以下错误:
1) Key pages viewing keys on the pending keys page for an admin
Failure/Error: @admin = FactoryGirl.create(:admin)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
# ./spec/requests/key_pages_spec.rb:150:in `block (5 levels) in <top (required)>'
2) Key pages viewing keys on the pending keys page for an admin
Failure/Error: let!(:unpublished_key) { FactoryGirl.create(:key) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
# ./spec/requests/key_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
对于和弦:
1) Chord pages viewing chords on the pending chords page for an admin
Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
# ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
2) Chord pages viewing chords on the pending chords page for an admin does not redirect to the sign up page
Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
# ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
3) Chord pages viewing chords on the pending chords page for an admin
Failure/Error: let!(:unpublished_chord) { FactoryGirl.create(:chord) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username has already been taken
# ./spec/requests/chord_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
编辑:添加规范
require 'spec_helper'
describe Key do
let(:user) { FactoryGirl.create(:user) }
let(:key) { FactoryGirl.create(:key, user: user) }
let(:chord) { FactoryGirl.create(:chord, user: user) }
let(:chord_2) { FactoryGirl.create(:chord, user: user) }
let(:c_chord) { FactoryGirl.create(:chord, user: user) }
let(:note) { FactoryGirl.create(:note) }
let(:note_2) { FactoryGirl.create(:note) }
let(:note_3) { FactoryGirl.create(:note) }
let(:note_4) { FactoryGirl.create(:note) }
subject { key }
describe "its attributes" do
it { should respond_to(:name) }
describe "when name is blank" do
before { key.name = nil }
it { should_not be_valid }
end
it { should respond_to(:description) }
describe "when description is blank" do
before { key.description = nil }
it { should_not be_valid }
end
it { should respond_to(:user_id) }
it { should respond_to(:user) }
its(:user) { should eq user }
describe "when user_id is not present" do
before { key.user_id = nil }
it { should_not be_valid }
end
it { should be_valid }
it { should respond_to(:keychords) }
it { should respond_to(:chords) }
it { should respond_to(:state) }
it { should respond_to(:submit) }
it { should respond_to(:withdraw) }
it { should respond_to(:approve) }
it { should respond_to(:unpublish) }
it { should respond_to(:root_note) }
it { should respond_to(:accidental) }
it { should respond_to(:third) }
describe "when root_note is blank" do
before { key.root_note = nil }
it { should_not be_valid }
end
describe "when root_note is not in root_noteS" do
before { key.root_note = 'X' }
it { should_not be_valid }
end
describe "when accidental is blank" do
before { key.accidental = nil }
it { should_not be_valid }
end
describe "when accidental is not in ACCIDENTALS" do
before { key.accidental = 'Not an Accidental' }
it { should_not be_valid }
end
describe "when third is blank" do
before { key.third = nil }
it { should_not be_valid }
end
describe "when third is not in THIRDS" do
before { key.third = 'Not in Thirds' }
it { should_not be_valid }
end
end
describe "state changes" do
it "should change from unpublished to submitted when submitted" do
expect{ key.submit }.to change{key.state}.from('Unpublished').to('Submitted')
end
it "should change from submitted to unpublished when withdrawn" do
key.state = 'Submitted'
expect{ key.withdraw }.to change{key.state}.from('Submitted').to('Unpublished')
end
it "should change from submitted to approved when approved" do
key.state = 'Submitted'
expect{ key.approve }.to change{key.state}.from('Submitted').to('Published')
end
it "should change from submitted to unpublished when rejected" do
key.state = 'Submitted'
expect{ key.reject }.to change{key.state}.from('Submitted').to('Unpublished')
end
it "should change from published to unpublished when unpublished" do
key.state = 'Published'
expect{ key.unpublish }.to change{key.state}.from('Published').to('Unpublished')
end
end
describe "its chords" do
it "should not have any chords" do
key.chords.count.should == 0
end
it "should not has_chord?" do
expect(key.haschord?(chord)).to be_false
end
it "add chords should change number of chords" do
expect { key.addchord!(chord) }.to change{key.chords.count}.from(0).to(1)
end
describe "after adding a chord" do
before { key.addchord!(chord) }
it "should add the chord" do
expect(key.chords).to include(chord)
end
it "should only have the added chord" do
expect(key.chords).not_to include(chord_2)
end
it "should haschord?(chord)" do
expect(key.haschord?(chord)).to be_true
end
it "should not haschord?(chord2)" do
expect(key.haschord?(chord_2)).to be_false
end
end
end
describe "its notes" do
before do
chord.notes << [note, note_2]
chord_2.notes << [note_3, note_4]
end
describe "before adding chords" do
it "should not have any notes" do
key.notes.count.should == 0
end
it "should not hasnote?(note)" do
expect(key.hasnote?(note)).to be_false
end
end
describe "after adding a chord" do
before { key.chords << chord }
it "should have notes" do
key.notes.count.should == 2
end
it "should have notes 1 and 2" do
expect(key.notes).to include(note, note_2)
end
it "should not have notes 3 or 4" do
expect(key.notes).not_to include(note_3, note_4)
end
it "should hasnote?(note)" do
expect(key.hasnote?(note)).to be_true
end
end
end
end
【问题讨论】:
-
向我们展示您的测试文件。您是否也将
spec_helper.rb中的use_transaction_fixtures设置为true? -
@Vimsha,我添加了生成第一个错误的规范 (
key_pages_spec.rb)。use_transactional_fixtures = true在我的spec_helper中。谢谢
标签: ruby-on-rails ruby rspec factory-bot