Created
January 23, 2015 05:35
-
-
Save rajputvai/e61af268fdc3bdd584d1 to your computer and use it in GitHub Desktop.
FactoryGirl Association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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