Sha256: 0e9e25027ca6c3247244ba0415330416816c6a47363da0e967ebfe391fa43841

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'

describe "api_cache" do
  before :each do
    FakeWeb.register_uri(:get, "http://www.google.com/", :body => "Google")

    APICache.store = nil
  end

  it "should work when url supplied" do
    APICache.get('http://www.google.com/').should =~ /Google/
  end

  it "should work when block supplied" do
    APICache.get('foobar') do
      42
    end.should == 42
  end

  it "should raise error raised in block unless key available in cache" do
    lambda {
      APICache.get('foo') do
        raise RuntimeError, 'foo'
      end
    }.should raise_error(RuntimeError, 'foo')

    APICache.get('foo', :period => 0) do
      'bar'
    end

    lambda {
      APICache.get('foo') do
        raise RuntimeError, 'foo'
      end
    }.should_not raise_error
  end

  it "should raise APICache::TimeoutError if the API call times out unless data available in cache" do
    lambda {
      APICache.get('foo', :timeout => 1) do
        sleep 1.1
      end
    }.should raise_error APICache::TimeoutError, 'Timed out when calling API (timeout 1s)'

    APICache.get('foo', :period => 0) do
      'bar'
    end

    lambda {
      APICache.get('foo', :timeout => 1) do
        sleep 1.1
      end
    }.should_not raise_error
  end

  it "should return a default value rather than raising an exception if :fail passed" do
    APICache.get('foo', :fail => "bar") do
      raise 'foo'
    end.should == 'bar'
  end

  it "should accept a proc to fail" do
    APICache.get('foo', :fail => lambda { "bar" }) do
      raise 'foo'
    end.should == 'bar'
  end

  it "should accept nil values for :fail" do
    APICache.get('foo', :fail => nil) do
      raise 'foo'
    end.should == nil
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mloughran-api_cache-0.2.0 spec/integration_spec.rb
api_cache-0.2.0 spec/integration_spec.rb