Factory_girls 序列和唯一性
我在factory.rb 中有这个设置。
Factory.sequence(:email) { |n| "email#{n}@factory.com" }
Factory.sequence(:username) { |n| "username_#{n}" }
Factory.define :user do |u|
u.email { Factory.next :email }
u.username { Factory.next :username }
u.first_name 'Ivan'
u.last_name 'Pupkin'
u.latitude '42'
u.longitude '-71'
u.password 'qwerty'
u.password_confirmation 'qwerty'
end
当我创建 Factory(:users) 的两个实例时,出现唯一性错误。
describe CartsController do
let(:user) { Factory(:user) }
let(:another_user) { Factory(:user) }
let(:cart) { Factory(:cart) }
describe 'show my cart' do
before { sign_in user}
before { get :show, :id => user.carts.last }
it { should respond_with :success }
end
describe 'show different person cart' do
before { sign_in user }
before { get :show, :id => another_user.carts.last}
it { should respond_with :redirect }
end
end
我的问题出在哪里?
Failure/Error: let(:user) { Factory(:user) }
Validation failed: Username has already been taken, Email has already been taken
I have this setup in the factories.rb.
Factory.sequence(:email) { |n| "email#{n}@factory.com" }
Factory.sequence(:username) { |n| "username_#{n}" }
Factory.define :user do |u|
u.email { Factory.next :email }
u.username { Factory.next :username }
u.first_name 'Ivan'
u.last_name 'Pupkin'
u.latitude '42'
u.longitude '-71'
u.password 'qwerty'
u.password_confirmation 'qwerty'
end
When i creating two instances of the Factory(:users) i got uniqueness error.
describe CartsController do
let(:user) { Factory(:user) }
let(:another_user) { Factory(:user) }
let(:cart) { Factory(:cart) }
describe 'show my cart' do
before { sign_in user}
before { get :show, :id => user.carts.last }
it { should respond_with :success }
end
describe 'show different person cart' do
before { sign_in user }
before { get :show, :id => another_user.carts.last}
it { should respond_with :redirect }
end
end
Where is my problem?
Failure/Error: let(:user) { Factory(:user) }
Validation failed: Username has already been taken, Email has already been taken
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数据库中似乎有记录,因为
let(:user) { Factory(:user) }
失败,而不是let(:another_user) { Factory(:user) }
> 所以我看到两种可能的解决方案:在顶部添加User.delete_all
或手动清理数据库It seems that there are records in your DB because fails
let(:user) { Factory(:user) }
and notlet(:another_user) { Factory(:user) }
so I see two possible solutions: addUser.delete_all
in the top or manually clean DB是的,它是测试数据库中的剩余部分。您应该在
spec_helper.rb
中包含以下行:请注意,即使您使用的是工厂而不是固定装置,这也是有用的。它将每个示例包装在数据库事务中,以便您每次都有一个干净的状态。如果您不使用 ActiveRecord,或者由于任何其他原因事务无法为您工作,则需要引入诸如
database_cleaner
gem 之类的东西来在每次测试运行后清理测试数据库。Yeah, it's leftovers in the test db. You should have the following line in
spec_helper.rb
:Note that this is useful even though you are using factories rather than fixtures. It wraps each example in a database transaction so that you have a clean slate every time. If you aren't using ActiveRecord, or if transactions are not going to work for you for any other reason, you need to pull in something like the
database_cleaner
gem to clean up the test database after each test run.