diff options
| author | Robert | 2017-10-21 10:20:33 +0200 | 
|---|---|---|
| committer | Robert | 2017-10-21 10:25:30 +0200 | 
| commit | e3f3d13b6c096f7dcd0e5429efc46a70c7da7e7c (patch) | |
| tree | 97482183a7772839dd5681c6756052c4be108b59 | |
| parent | a91d34f1ab09ba90fc0e75d64c091adc74052746 (diff) | |
| download | chouette-core-e3f3d13b6c096f7dcd0e5429efc46a70c7da7e7c.tar.bz2 | |
Faster Spex
  - Clean object cache before each example with type :faster
  - Started to work on FactroyGirl integration
N.B. While FactoryGirl support right now only includes defaults it slows down the ActiveRecord based stubbing by a factor of ~2.5
which might be fixable if worth it.
| -rw-r--r-- | spec/models/faster_specs_spec.rb | 8 | ||||
| -rw-r--r-- | spec/support/faster/model_stubber.rb | 23 | ||||
| -rw-r--r-- | spec/support/faster/model_stubber/object_cache.rb | 4 | 
3 files changed, 31 insertions, 4 deletions
| diff --git a/spec/models/faster_specs_spec.rb b/spec/models/faster_specs_spec.rb index 31f31e0f8..edae5f485 100644 --- a/spec/models/faster_specs_spec.rb +++ b/spec/models/faster_specs_spec.rb @@ -1,6 +1,6 @@  RSpec.describe 'Faster Specs', type: :faster do -  N = (ENV['N'] || 50).to_i +  N = (ENV['N'] || 1).to_i    shared_examples_for 'correct behavior' do @@ -28,7 +28,13 @@ RSpec.describe 'Faster Specs', type: :faster do      let( :referential ){ stub_model( Referential, workbench: workbench ) }      it_behaves_like 'correct behavior' +  end + +  context 'with FactoryGirl' do  +    let( :workbench ){ stub_model :workbench } +    let( :referential ){ stub_model( :referential, workbench: workbench ) } +    it_behaves_like 'correct behavior'    end    context 'meta' do diff --git a/spec/support/faster/model_stubber.rb b/spec/support/faster/model_stubber.rb index 789f5c2d2..3604c8ca4 100644 --- a/spec/support/faster/model_stubber.rb +++ b/spec/support/faster/model_stubber.rb @@ -1,15 +1,36 @@  require_relative 'model_stubber/implementation'  module ModelStubber -  def stub_model klass, **params + +  def stub_model builder, **params +    case builder +    when Symbol +      stub_model_with_fg builder, params +    else +      stub_model_with_ar builder, params +    end +  end + +  private # symbolic as we are included + +  def stub_model_with_ar klass, **params      klass.new.tap do | model |        Implementation.new(model, params).setup      end    end + +  def stub_model_with_fg factory, **params +    build(factory).tap do | model | +      Implementation.new(model, params).setup +    end +  end  end  RSpec.configure do | conf |    # Empty Helper's cache before each example or create a helper?    conf.include ModelStubber, type: :faster +  conf.before(:each, type: :faster) do +    ModelStubber::ObjectCache.empty_cache! +  end  end diff --git a/spec/support/faster/model_stubber/object_cache.rb b/spec/support/faster/model_stubber/object_cache.rb index 5749be76f..8f24dcf71 100644 --- a/spec/support/faster/model_stubber/object_cache.rb +++ b/spec/support/faster/model_stubber/object_cache.rb @@ -7,13 +7,13 @@ module ModelStubber      end      def empty_cache! -       @__cache__ = _cache +      @__cache__ = _cache      end      private      def cache -       @__cache__ ||= _cache  +      @__cache__ ||= _cache       end      def _cache        Hash.new { |h, k| h[k] = {} } | 
