制造商在验证时失败了mongoid

时间:2011-08-04 01:44:48

标签: ruby-on-rails unit-testing mongodb mongoid

我无法运行任何测试,因为我尝试制作的每个用户都会生成验证错误。

我有以下测试。

class BillTest < ActiveSupport::TestCase

  def setup
    load_all_sponsors
    @the_bill = Bill.new(:govtrack_name => "h1")
    @user1 = Fabricate(:user1)
    @user2 = Fabricate(:user2)

我的制作定义如下:

Fabricator(:user) do
   email      'another_user_here@domain.com'
   name       'anew user'
   roles_mask 1
   password   "secret"
   password_confirmation "secret"
   polco_groups {[Fabricate(:polco_group, :name => 'foreign', :type => :custom)]}
end

Fabricator(:admin, :class_name => :user) do
  email 'admin@yourdomain.com'
  name  'Administrator'
  roles_mask  5
  password   "secret"
  password_confirmation "secret"
end

Fabricator(:registered, :class_name => :user) do
   email      'registered_user@domain.com'
   name       'Tim TheRegistered'
   roles_mask 2
   password   "the_secret"
   password_confirmation "the_secret"
   polco_groups {[Fabricate(:polco_group, {:name => 'AL', :type => :state}),
              Fabricate(:polco_group, {:name => 'AL01', :type => :district}),
              Fabricate(:polco_group, {:name => "Kirk\'s Kids" , :type => :custom})]}
end

Fabricator(:user1, :class_name => :user) do
   email      'user1@domain.com'
   name       'User1'
   roles_mask 2
   password   "the_big_secret"
   password_confirmation "the_big_secret"
   polco_groups {[Fabricate(:polco_group, {:name => 'AL', :type => :state}),
              Fabricate(:polco_group, {:name => 'AL01', :type => :district}),
              Fabricate(:polco_group, {:name => "Kirk\'s Kids" , :type => :custom})]}
end

Fabricator(:user2, :class_name => :user) do
   email      'user2@domain.com'
   name       'User2'
   roles_mask 2
   password   "the_big_secret"
   password_confirmation "the_big_secret"
   polco_groups {[Fabricate(:polco_group, {:name => 'AL', :type => :state}),
              Fabricate(:polco_group, {:name => 'FL01', :type => :district}),
              Fabricate(:polco_group, {:name => "Ft. Sam Washington 1st Grade" , :type => :custom})]}
end

无论哪个测试运行,我都会得到相同的错误

  9) Error:
test_update_from_directory(BillTest):
Mongoid::Errors::Validations: Validation failed - Email is already taken, Email is already taken, Name is already taken.
    test/unit/bill_test.rb:8:in `setup'

我尝试了许多不同的制造商,以各种不同的方式继承,没有任何东西可以通过这个错误。我非常渴望得到这方面的帮助。

2 个答案:

答案 0 :(得分:0)

您应该成为“ffaker”gem的用户,以在您的规范中生成电子邮件地址等。此外,您应该继承先前定义的制造商的字段,如下所示。

Fabricator(:user) do
 email      { Faker::Internet.email }
 name       { Faker::Name.name }
 roles_mask 1
 password   "secret"
 password_confirmation "secret"
 polco_groups {[Fabricate(:polco_group, :name => 'foreign', :type => :custom)]}
end

Fabricator(:admin, :from => :user) do
  roles_mask  5
end

Fabricator(:registered, :from => :user) do
  roles_mask 2
  polco_groups {[Fabricate(:polco_group, :name => 'AL', :type => :state),
          Fabricate(:polco_group, :name => 'AL01', :type => :district),
          Fabricate(:polco_group, :name => "Kirk\'s Kids" , :type => :custom)]}
end

Fabricator(:user1, :from => :registered)

Fabricator(:user2, :from => :registered) do
  polco_groups {[Fabricate(:polco_group, {:name => 'AL', :type => :state}),
          Fabricate(:polco_group, {:name => 'FL01', :type => :district}),
          Fabricate(:polco_group, {:name => "Ft. Sam Washington 1st Grade" , :type => :custom})]}
end

它与您的问题无关,但看起来user1和user2应该只是调用Fabricate而不是实际定义为Fabricators。我不建议在Fabricator中添加那么多明确的信息。您应该只定义生成有效对象所必需的。任何特定数据都应该在特定规范的服务中。

答案 1 :(得分:0)

在遇到Mongoid的Machinist和Fabricator问题后,我已经选择了FactoryGirl - 语法感觉就像退后一步 - 但它确实有效。

我与其他人的问题与验证,STI和多态性有关。