Sha256: 9118a16ec4ecb1f324826bfc103da9e3518ead27b849c870e38fb3c3e7b6efc4

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
frenetic-0.0.20.alpha.3 spec/concerns/breifly_memoizable_spec.rb
frenetic-0.0.20.alpha.2 spec/concerns/breifly_memoizable_spec.rb