gpt4 book ai didi

ruby-on-rails - factory_girls 序列和唯一性

转载 作者:行者123 更新时间:2023-11-28 20:05:09 26 4
gpt4 key购买 nike

我在 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

当我创建 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

最佳答案

您的数据库中似乎有记录,因为 let(:user) { Factory(:user) } 而不是 let(:another_user) { Factory(:user) } 所以我看到了两种可能的解决方案:在顶部添加 User.delete_all 或手动清理 DB

关于ruby-on-rails - factory_girls 序列和唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5993285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com