diff options
| author | Robert | 2017-10-24 08:40:14 +0200 | 
|---|---|---|
| committer | Robert | 2017-10-24 08:40:14 +0200 | 
| commit | c7e41672ee216aea8d905771b2672bb32b120c0c (patch) | |
| tree | 1667c0c3daae3bc04a55b2a32c1f4d3b5059b2b4 | |
| parent | c5e5e5bf1a7429dfa72bbd2f3d48eec7cac442a6 (diff) | |
| download | chouette-core-c7e41672ee216aea8d905771b2672bb32b120c0c.tar.bz2 | |
bup [amend me]faster-spex
| -rw-r--r-- | spec/models/faster_specs_spec.rb | 26 | ||||
| -rw-r--r-- | spec/support/faster/model_stubber/object_cache.rb | 18 | ||||
| -rw-r--r-- | spec/support/faster/model_stubber/wrapper.rb | 13 | 
3 files changed, 40 insertions, 17 deletions
| diff --git a/spec/models/faster_specs_spec.rb b/spec/models/faster_specs_spec.rb index 1037e4487..eb7a7a5fe 100644 --- a/spec/models/faster_specs_spec.rb +++ b/spec/models/faster_specs_spec.rb @@ -5,27 +5,31 @@ RSpec.describe 'Faster Specs', type: :faster do    shared_examples_for 'correct behavior' do      N.times do -      it 'finds workbench' do +      it 'finds associated workbench' do          expect( referential.workbench ).to eq(workbench)        end -      it 'finds referentials' do +      it 'finds associated referentials' do          expect( workbench.referentials ).to eq([referential])        end + +      it 'finds models from class' do +        expect( Workbench.find(workbench.id) ).to eq(workbench) +      end      end    end -  context 'in DB' do  -    let( :workbench ){ create :workbench } -    let( :referential ){ create(:referential, workbench: workbench) } +  context 'stubbed' do  +    let( :workbench ){ stub_model Workbench } +    let( :referential ){ stub_model( Referential, workbench: workbench ) }      it_behaves_like 'correct behavior'    end -  context 'stubbed' do  -    let( :workbench ){ stub_model Workbench } -    let( :referential ){ stub_model( Referential, workbench: workbench ) } +  context 'in DB' do  +    let( :workbench ){ create :workbench } +    let( :referential ){ create(:referential, workbench: workbench) }      it_behaves_like 'correct behavior'    end @@ -71,11 +75,5 @@ RSpec.describe 'Faster Specs', type: :faster do      let( :workbench ){ stub_model :fast_workbench }      it_behaves_like 'meta', :organisation - -    it 'check for associations in FactoryGirl' do -      require 'pry'; binding.pry -       -    end -    end  end diff --git a/spec/support/faster/model_stubber/object_cache.rb b/spec/support/faster/model_stubber/object_cache.rb index 8f24dcf71..9fd71d864 100644 --- a/spec/support/faster/model_stubber/object_cache.rb +++ b/spec/support/faster/model_stubber/object_cache.rb @@ -2,8 +2,14 @@ module ModelStubber    module ObjectCache extend self      def add_to_cache model -      model.id = cache[model.class].keys.last.try(:succ) || 1 -      cache[model.class].update( model.id => model ) +      if class_cache = cache[model.class] +        model.id = class_cache.keys.last.try(:succ) || 10_001 +        class_cache.update( model.id => model ) +      else +        cache[model.class] = {} +        stub_class model.class +        add_to_cache model +      end      end      def empty_cache! @@ -16,8 +22,14 @@ module ModelStubber        @__cache__ ||= _cache       end      def _cache -      Hash.new { |h, k| h[k] = {} } +      Hash.new      end +    def stub_class klass +      kache = cache +      class << klass; self end.module_eval do +        define_method(:find){ |id| kache[klass].fetch(id) }  +      end +    end    end  end diff --git a/spec/support/faster/model_stubber/wrapper.rb b/spec/support/faster/model_stubber/wrapper.rb new file mode 100644 index 000000000..25ca374dc --- /dev/null +++ b/spec/support/faster/model_stubber/wrapper.rb @@ -0,0 +1,13 @@ +module ModelStubber +  # I am wrapping ActiveRecord subclasses in order not to mess with +  # ActiveRecord itself +  class Wrapper + +    attr_reader :klass + +    def initialize klass +      @klass = klass +    end +     +  end +end | 
