1
votes

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      '[email protected]'
   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 '[email protected]'
  name  'Administrator'
  roles_mask  5
  password   "secret"
  password_confirmation "secret"
end

Fabricator(:registered, :class_name => :user) do
   email      '[email protected]'
   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      '[email protected]'
   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      '[email protected]'
   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.

2

2 Answers

0
votes

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.

0
votes

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.