Sha256: 5d08513b15bedfb8e3bf3ab824de758e011b89f4c6a3b2c18053102f7a213189

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'

describe "Factory" do
  before do
    factory.registry.clear
    factory.counters.clear
  end
  after{remove_constants :User, :ATmp, :BTmp}

  it "should build objects from definition" do
    factory.define :user, class: 'OpenObject' do |u|
      u.name = 'an_user'
    end

    factory.build(:user, password: 'abc'){|u| u.roles = %w(user)}.should ==
      {name: 'an_user', password: 'abc', roles: %w(user)}.to_openobject
  end

  it "should provide parent to represent hierarchies" do
    factory.define :user, class: 'OpenObject' do |u|
      u.name = 'an_user'
    end

    factory.define :manager, parent: :user do |u|
      u.name = 'a_manager'
    end

    factory.build(:manager, password: 'abc'){|u| u.roles = %w(manager)}.should ==
      {name: 'a_manager', password: 'abc', roles: %w(manager)}.to_openobject
  end

  it "should create create objects" do
    stub = self.stub
    factory.define :user, class: stub
    factory.define :manager, parent: :user

    stub.should_receive(:new).once.and_return stub
    stub.should_receive(:save!).once
    factory.create :manager
  end

  it "should validate input" do
    -> {factory.define :user}.should raise_error(/provided for :user/)
  end

  it "should provide counters" do
    factory.next(:id).should == 0
    factory.next(:id).should == 1
  end

  it "should use correct creation method (build / create) in associations" do
    class ATmp
      attr_accessor :b
      def save!; end
    end

    class BTmp
      attr_accessor :saved
      def save!
        @saved = true
      end
    end

    factory.define :a, class: 'ATmp' do |o|
      # b will be either build or create, method will be choosen automatically.
      o.b = factory :b
    end
    factory.define :b, class: 'BTmp'

    factory.build(:a).b.saved.should be_nil
    factory.create(:a).b.saved.should be_true
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_ext-4.0.3 spec/rspec_ext/factory_spec.rb