Sha256: 9833458908986b6d885045f2e066406c7d0228665c2fd49551f9a760d03c907b

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

# encoding: utf-8
require 'spec_helper'
require 'formtastic/namespaced_class_finder'

describe Formtastic::NamespacedClassFinder do
  include FormtasticSpecHelper

  before do
    stub_const('SearchPath', Module.new)
  end

  let(:search_path) { [ SearchPath ] }
  subject(:finder) { Formtastic::NamespacedClassFinder.new(search_path) }

  shared_examples 'Namespaced Class Finder' do
    subject(:found_class) { finder.find(:custom_class) }

    context 'Input defined in the Object scope' do
      before do
        stub_const('CustomClass', Class.new)
      end

      it { expect(found_class).to be(CustomClass) }
    end

    context 'Input defined in the search path' do
      before do
        stub_const('SearchPath::CustomClass', Class.new)
      end

      it { expect(found_class).to be(SearchPath::CustomClass) }
    end

    context 'Input defined both in the Object scope and the search path' do
      before do
        stub_const('CustomClass', Class.new)
        stub_const('SearchPath::CustomClass', Class.new)
      end

      it { expect(found_class).to be(SearchPath::CustomClass) }
    end

    context 'Input defined outside the search path' do
      before do
        stub_const('Foo', Module.new)
        stub_const('Foo::CustomClass', Class.new)
      end

      let(:error) { Formtastic::NamespacedClassFinder::NotFoundError }

      it { expect { found_class }.to raise_error(error) }
    end
  end

  context '#finder' do
    before do
      Rails.application.config.stub(:cache_classes).and_return(cache_classes)
    end

    context 'when cache_classes is on' do
      let(:cache_classes) { true }
      it_behaves_like 'Namespaced Class Finder'
    end

    context 'when cache_classes is off' do
      let(:cache_classes) { false }
      it_behaves_like 'Namespaced Class Finder'
    end
  end

  context '#find' do
    it 'caches calls' do
      expect(subject).to receive(:resolve).once.and_call_original
      subject.find(:object)
      subject.find(:object)
    end
  end

end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
formtastic-3.1.5 spec/namespaced_class_finder_spec.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/formtastic-3.1.4/spec/namespaced_class_finder_spec.rb
formtastic-3.1.4 spec/namespaced_class_finder_spec.rb
formtastic-3.1.3 spec/namespaced_class_finder_spec.rb
formtastic-3.1.2 spec/namespaced_class_finder_spec.rb
formtastic-3.1.1 spec/namespaced_class_finder_spec.rb
formtastic-3.1.0 spec/namespaced_class_finder_spec.rb
formtastic-3.1.0.rc2 spec/namespaced_class_finder_spec.rb
formtastic-3.1.0.rc1 spec/namespaced_class_finder_spec.rb