Sha256: 8dc9600b813ba80ce7c74211cf94576692b0f307be165ecb64c6a9eccd84ad65

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'spec_helper'

describe Frenetic::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
        allow(instance).to receive(:external_call).and_call_original
      end

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

        it 'is called' do
          instance.fetch
          expect(instance).to have_received(:external_call).once
        end
      end

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

        it 'is not called' do
          instance.fetch
          expect(instance).to_not have_received(:external_call)
        end

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

          it 'is called' do
            instance.fetch
            expect(instance).to have_received(:external_call).once
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frenetic-1.0.0.alpha.1 spec/briefly_memoizable_spec.rb