我从以下工厂生成用户、客户和发票
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "person_#{n}@example.com" }
password "foobar"
password_confirmation "foobar"
end
factory :client do
sequence(:name) { |n| "Client Name #{n}" }
sequence(:address1) { |n| "#{n} Street name" }
sequence(:address2) { |n| "#{n} Area name" }
city "Chennai"
state "Tamil Nadu"
country "India"
zipcode "600001"
contactname "Sathish Manohar"
sequence(:email) { |n| "person_#{n}@example.com"}
user
end
factory :invoice do
client
end
end
我有一些要求和型号规格,以下是我的主要要求规格:
https://gist.github.com/4418869-clients_pages_spec.rb
https://gist.github.com/4418876-invoices_pages_spec.rb _ _
在调用users
中删除after(:all) { User.delete_all }
所有内容时,所有测试在运行时都通过,以便使用bundle exec rspec spec --order default
当所有users
都没有被删除时after(:all) { User.delete_all }
,运行测试时会出现电子邮件唯一性错误。这是设计验证每个用户电子邮件的唯一性的错误,所以我认为在第二次运行测试时,factory_girl
会尝试创建相同的用户。
这是错误消息:
/home/sathish/.rvm/gems/ruby-1.9.3-p194@invoice_jet/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)
如何摆脱此错误消息,并创建设计用户以进行正确的测试?
背景信息:
spec_helper.rb
使用mysql作为数据库所有表都config.use_transactional_fixtures = true
使用InnoDB
引擎