Sha256: a00d1f51c924d59728e052d4068846fd5907bfb4c683b6b7c975aaab7286abc9

Contents?: true

Size: 1.4 KB

Versions: 10

Compression:

Stored size: 1.4 KB

Contents

require "spec_helper"

describe "syntax methods within dynamic attributes" do
  before do
    define_model("Post", title: :string, user_id: :integer) do
      belongs_to :user

      def generate
        "generate result"
      end
    end
    define_model("User", email: :string)

    FactoryGirl.define do
      sequence(:email_address) { |n| "person-#{n}@example.com" }

      factory :user do
        email { generate(:email_address) }
      end

      factory :post do
        title { generate }
        user { build(:user) }
      end
    end
  end

  it "can access syntax methods from dynamic attributes" do
    expect(FactoryGirl.build(:user).email).to eq "person-1@example.com"
    expect(FactoryGirl.attributes_for(:user)[:email]).to eq "person-2@example.com"
  end

  it "can access syntax methods from dynamic attributes" do
    expect(FactoryGirl.build(:post).user).to be_instance_of(User)
  end

  it "can access methods already existing on the class" do
    expect(FactoryGirl.build(:post).title).to eq "generate result"
    expect(FactoryGirl.attributes_for(:post)[:title]).to be_nil
  end

  it 'allows syntax methods to be used when the block has an arity of 1' do
    FactoryGirl.define do
      factory :post_using_block_with_variable, parent: :post do
        user { |_| build(:user) }
      end
    end

    expect(FactoryGirl.build(:post_using_block_with_variable).user).to be_instance_of(User)
  end
end

Version data entries

10 entries across 7 versions & 4 rubygems

Version Path
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
factory_girl-4.5.0 spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
factory_girl-4.4.0 spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb