aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/models/faster_specs_spec.rb26
-rw-r--r--spec/support/faster/model_stubber/object_cache.rb18
-rw-r--r--spec/support/faster/model_stubber/wrapper.rb13
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