Sha256: 83b5accb56407976a2872198268cf8aa4c55279cef4dd7bc1c6be7758045557d

Contents?: true

Size: 1.9 KB

Versions: 13

Compression:

Stored size: 1.9 KB

Contents

require 'spec_helper'

describe "a stubbed instance" do
  include FactoryGirl::Syntax::Methods

  before do
    define_model('User')

    define_model('Post', :user_id => :integer) do
      belongs_to :user
    end

    FactoryGirl.define do
      factory :user

      factory :post do
        user
      end
    end
  end

  subject { build_stubbed(:post) }

  it "acts as if it came from the database" do
    should_not be_new_record
  end

  it "assigns associations and acts as if it is saved" do
    subject.user.should be_kind_of(User)
    subject.user.should_not be_new_record
  end
end

describe "a stubbed instance overriding strategy" do
  include FactoryGirl::Syntax::Methods

  def define_factories_with_method
    FactoryGirl.define do
      factory :user

      factory :post do
        association(:user, :method => :build)
      end
    end
  end

  def define_factories_with_strategy
    FactoryGirl.define do
      factory :user

      factory :post do
        association(:user, :strategy => :build)
      end
    end
  end

  before do
    define_model('User')
    define_model('Post', :user_id => :integer) do
      belongs_to :user
    end
  end

  context "associations declared with :strategy" do
    before  { define_factories_with_strategy }
    subject { build_stubbed(:post) }

    it "acts as if it is saved in the database" do
      should_not be_new_record
    end

    it "assigns associations and acts as if it is saved" do
      subject.user.should be_kind_of(User)
      subject.user.should_not be_new_record
    end
  end

  context "associations declared with :method" do
    before  { define_factories_with_method }
    subject { build_stubbed(:post) }

    it "acts as if it is saved in the database" do
      should_not be_new_record
    end

    it "assigns associations and acts as if it is saved" do
      subject.user.should be_kind_of(User)
      subject.user.should_not be_new_record
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/factory_girl-2.6.4/spec/acceptance/stub_spec.rb
challah-0.6.2 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
challah-0.6.1 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
factory_girl-2.6.4 spec/acceptance/stub_spec.rb
factory_girl-2.6.3 spec/acceptance/stub_spec.rb
factory_girl-2.6.2 spec/acceptance/stub_spec.rb
challah-0.5.1 vendor/bundle/gems/factory_girl-2.6.1/spec/acceptance/stub_spec.rb
factory_girl-2.6.1 spec/acceptance/stub_spec.rb
factory_girl-2.6.0 spec/acceptance/stub_spec.rb