开发者

Fabricators failing with mongoid from validation

I can't get my any test to run because every user I try to fabricate generates validation errors.

I have the following test.

class BillTest < ActiveSupport::TestCase

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

My fabrications are defined by:

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

No matter which test runs, I get the same error

  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'

I have tried a number of different fabricators, inheriting in all kinds of different ways and nothing gets past this error. I'm pretty desperate for help on this one.


You should be user the "ffaker" gem to generate email addresses and the like in your specs. Also, you should be inheriting fields form previously defined fabricators, as illustrated below.

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

It is unrelated to your issue, but it looks like user1 and user2 should just be calls to Fabricate and not actually defined as Fabricators. I don't recommend putting that much explicit information in the Fabricator. You should only be defining was is necessary to generate a valid object. Any specific data should be in the service of a particular spec.


After running in to Machinist and Fabricator issues with Mongoid I've settled on FactoryGirl - the syntax feels like a step back - but it works.

The issues I had with the others were related to validation, STI and polymorphism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜