Factory Girl... It's like a Dish washer.

I’ll probably come here the most to grab this line of code include FactoryGirl::Syntax::Methods

Factory Girl. I do not recommend learning Factory Girl until you’re comfortable writing your first few tests. Or in my case, first 500 + tests. Most everything that you’ll be doing with Factory girl is to make your life easier, not necessarily to create some sort of functionality. See what I’m saying? Or not. Probably not. Factory Girl makes life easier. It’s like a dish washer. Can you wash dishes by hand? Of course, but why was dishes by hand when you can use the dish washer and make your life easier? The same with FactoryGirl. You can write the same tests, but with Factory Girl, it can be a little easier. Factory Girl will DRY up your test code while staying explicit enough for anyone to understand what’s going on.

However, if you’re learning to write your first test, and the tutorial you found suggests using Factory Girl, I recommend finding another tutorial as that will add un-necessary complexity which can be mixed in down the road. Sure, the ideal setup is using factory girl. But, comon hombre, let’s take it bit by bit. I realized that factory girl could help me after writing about 300 tests. I should of implemented it around 50 or 60, but definitely not at test 0, as I had no idea about testing. That is if it is your first time learning testing. However, on future projects, I plan to use FactoryGirl right away.

Anyways on to FACTORY GIRL.

But wait. Let’s give props to the maintainer of FactoryGirl. Look at his old-man cardigan. He’s owning the look.

This tutorial will in no way help you use Factory Girl, nor will it give you any concrete details on implementing FactoryGirl. It’s basically so I can remember how to get it working on my next rails project.

A great resource is https://upcase.com/videos/factory-girl

Implmentation

# spec/rails_helper.rb
require 'factory_girl'
include FactoryGirl::Syntax::Methods

  RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures" 
end

Example Factory #spec/factories/factories.rb FactoryGirl.define do factory :building do name “archertower” level 3 uniquebuilding_code { “#{name}1” } end

  factory :user do
    email "test54321@test.com"
    password "12345678"
    password_confirmation "12345678"
  end
end

Notice unique_building_code uses {}. That’s a dynamic variable. It’s always good to use that type of notation on time and dates because that will update the time and date every time a test is run.

The four core methods of FactoryGirl

build(:user, email:"derp@gmail.com")
attributes_for(:user, name:"Dan")
atttributes_for_pair(:user, name:"Dan")
build_stubbed(:user)

These methods will instantiate a new factory for you. Basically write the factory once, use it where ever in your test.

Dealing with Unique validations of attributes in the database

On users, email is typically required to be unique. Two users can’t have the same email right? Sometimes if you have a test that requires two users, if you use your one factory it will run into problems on email because it will have the same email. What do you do? Create two user factories right? NO! Just use sequence on the email attribute.

FactoryGirl.define do
  factory :user do
            name "dan"
    sequence(:email) { |n| "#{n}#{name}test54321@test.com" }
    password "12345678"
    password_confirmation "12345678"
  end
end

Associations in FactoryGirl

Imagine your user has an account. Account belongs_to User. Simply define your Account Factory then reference that shit within your User Factory.

    # spec/factories/factory.rb
FactoryGirl.define do
  factory :account do
    village_name "dawg"
  end

  factory :user do
    sequence(:email) { |n| "#{n}#{password}test@test.com" }
    password "12345678"
    password_confirmation "12345678"
    account
  end
    end

Now I can run something like this in my test… user_stub = build(:user) user_stub.account

Then I get the account attributes. For more information on Associations, see https://upcase.com/videos/factory-girl at 13:30 ish

I CANT TELL YOU HOW HUGE OF A PAIN IN THE ASS IT IS RIGHT NOW TO FUCKING GET FACTORY GIRL IMPLEMENTED ON MY RAILS APP

Sometimes I think about just blowing up my ClashProgress application. FUCK.

/Users/derp/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load': cannot load such file

I’ve gotten this stupid ass fucking error so many god damn times. It’s so fucking stupid. IT MEANS IM IN THE WRONG FUCKING FOLDER WHEN I CALL MY TESTS AND IT CANNOT FIND THE FUCKING TEST TO RUN. DONT FUCKING NAVIGATE TO A DIFFERENT FOLDER OTHER THAN APP ROOT WHEN TESTING

Post Content