Factory_girls 序列和唯一性

发布于 2024-11-06 15:55:01 字数 1121 浏览 4 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2024-11-13 15:55:01

您的数据库中似乎有记录,因为 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 not let(:another_user) { Factory(:user) } so I see two possible solutions: add User.delete_all in the top or manually clean DB

感性不性感 2024-11-13 15:55:01

是的,它是测试数据库中的剩余部分。您应该在 spec_helper.rb 中包含以下行:

  config.use_transactional_fixtures = true

请注意,即使您使用的是工厂而不是固定装置,这也是有用的。它将每个示例包装在数据库事务中,以便您每次都有一个干净的状态。如果您不使用 ActiveRecord,或者由于任何其他原因事务无法为您工作,则需要引入诸如 database_cleaner gem 之类的东西来在每次测试运行后清理测试数据库。

Yeah, it's leftovers in the test db. You should have the following line in spec_helper.rb:

  config.use_transactional_fixtures = true

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文