Sha256: 7c98a9d40b62ca29e3dbeb0d12ceb70dbce4abc5da483e999cdf382156d7d8c5

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

require 'spec_helper'

describe BrieflyMemoizable do
  let(:my_class) { Class.new }

  before do
    stub_const 'MyClass', my_class

    MyClass.send :include, described_class
    MyClass.class_eval do
      def fetch
        @fetch_age = Time.now + 3600

        external_call
      end
      briefly_memoize :fetch

      def external_call
        true
      end
    end
  end

  let(:instance) { MyClass.new }

  describe '.briefly_memoize' do
    context 'for an expensive method' do
      before do
        Timecop.freeze

        instance.fetch
      end

      context 'which is called again outside the memoize window' do
        before do
          Timecop.travel Time.now + 5400
        end

        it 'should be called' do
          instance.should_receive(:external_call).once.and_call_original

          instance.fetch
        end
      end

      context 'which is called again within the memoize window' do
        before do
          Timecop.travel Time.now + 1800
        end

        it 'should not be called' do
          instance.should_receive(:external_call).never.and_call_original

          instance.fetch
        end

        context 'after it has been reloaded' do
          before { instance.reload_fetch! }

          it 'should be called' do
            instance.should_receive(:external_call).once.and_call_original

            instance.fetch
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
frenetic-0.0.20.alpha.6 spec/concerns/breifly_memoizable_spec.rb
frenetic-0.0.20.alpha.5 spec/concerns/breifly_memoizable_spec.rb
frenetic-0.0.20.alpha.4 spec/concerns/breifly_memoizable_spec.rb