Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 91 lines (74 sloc) 2.573 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
require 'spec_helper'

describe "an instance generated by a factory that inherits from another factory" do
  before do
    define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string)

    FactoryGirl.define do
      factory :user do
        name "John"
        email { "#{name.downcase}@example.com" }
        login { email }

        factory :admin do
          name "admin"
          admin true
          upper_email { email.upcase }
        end

        factory :guest do
          email { "#{name}-guest@example.com" }
        end

        factory :no_email do
          email ""
        end

        factory :bill do
          name { "Bill" } #block to make attribute dynamic
        end
      end
    end
  end

  describe "the parent class" do
    subject { FactoryGirl.create(:user) }
    it { should_not be_admin }
    its(:email) { should eq "john@example.com" }
  end

  describe "the child class redefining parent's static method used by a dynamic method" do
    subject { FactoryGirl.create(:admin) }
    it { should be_kind_of(User) }
    it { should be_admin }
    its(:name) { should eq "admin" }
    its(:email) { should eq "admin@example.com" }
    its(:upper_email) { should eq "ADMIN@EXAMPLE.COM"}
  end

  describe "the child class redefining parent's dynamic method" do
    subject { FactoryGirl.create(:guest) }
    it { should_not be_admin }
    its(:name) { should eq "John" }
    its(:email) { should eql "John-guest@example.com" }
    its(:login) { should eq "John-guest@example.com" }
  end

  describe "the child class redefining parent's dynamic attribute with static attribute" do
    subject { FactoryGirl.create(:no_email) }
    its(:email) { should eq "" }
  end

  describe "the child class redefining parent's static attribute with dynamic attribute" do
    subject { FactoryGirl.create(:bill) }
    its(:name) { should eq "Bill" }
  end
end

describe "nested factories with different parents" do
  before do
    define_model("User", name: :string)

    FactoryGirl.define do
      factory :user do
        name "Basic User"

        factory :male_user do
          name "John Doe"
        end

        factory :uppercase_male_user, parent: :male_user do
          after(:build) { |user| user.name = user.name.upcase }
        end
      end
    end
  end

  it "honors :parent over the factory block nesting" do
    expect(FactoryGirl.build(:uppercase_male_user).name).to eq "JOHN DOE"
  end
end
Something went wrong with that request. Please try again.