Skip to content

Instantly share code, notes, and snippets.

@rajputvai
Created January 23, 2015 05:35
Show Gist options
  • Save rajputvai/e61af268fdc3bdd584d1 to your computer and use it in GitHub Desktop.
Save rajputvai/e61af268fdc3bdd584d1 to your computer and use it in GitHub Desktop.
FactoryGirl Association
# models/user.rb
class User < ActiveRecord::Base
has_many :user_brands
end
# models/user_brand.rb
class UserBrand < ActiveRecord::Base
belongs_to :user
end
# spec/factories/users.rb
FactoryGirl.define do
factory :user do
full_name "Joe Blow"
email "[email protected]"
password "p@ssw0rd"
amagi_db_id "6190"
trait :agency do
roles [Role.find_by_name("agency")]
end
end
end
# spec/factories/user_brands.rb
FactoryGirl.define do
factory :user_brand do
trait :agency do
client_name "Procter And Gamble Home Products Limited"
client_id "15041"
brand_name "Pantene Pro V Hair Oil"
end
trait :client do
client_name "Coca-Cola India Private Ltd."
client_id "15012"
brand_name "Fanta"
end
end
end
#spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, :type => :model do
it "agency should have atleast one brand" do
user = create(:user, :agency)
user_brands = user.user_brands.first
expect(user_brands).not_to be nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment