Sha256: 119df36a6bd1509052ca113f3faf1c0fa5b5f8e0b6b8ccd0218f62f3e5702362

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

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

class LazyCoverTest
  class << self
    attr_accessor :was_called
  end
end
describe SmokeMonster::LazyCover do

  it "should not call the block passed to it" do
    lazy = SmokeMonster::LazyCover.new(-> { raise 'was called' })
  end

  it "should call the block passed to it when things are references" do
    LazyCoverTest.was_called = false
    lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called = true })
    LazyCoverTest.was_called.must_equal false
    lazy.to_s
    LazyCoverTest.was_called.must_equal true
  end

  it "should call the block passed to it only once" do
    LazyCoverTest.was_called = 0
    lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called = LazyCoverTest.was_called + 1 })
    LazyCoverTest.was_called.must_equal 0
    lazy.to_s
    LazyCoverTest.was_called.must_equal 1
    lazy.to_s
    LazyCoverTest.was_called.must_equal 1
  end

  it "should return the same exceptions passed from its base class methods" do

    object = Object.new
    object.stubs(:defined_method).returns("yes")

    lazy = SmokeMonster::LazyCover.new(-> { object })
    lazy.defined_method.must_equal "yes"
    was_called = false
    begin
      lazy.not_a_defined_method
    rescue NoMethodError
      was_called = true
    end
    was_called.must_equal true
  end

  describe "the original subject" do
    it "should call the block passed to it" do
      SmokeMonster::LazyCover.new(-> { 0 }).the_original_subject.must_equal 0
      SmokeMonster::LazyCover.new(-> { "x" }).the_original_subject.must_equal "x"
    end

    it "should only call the block passed to it once" do
      LazyCoverTest.was_called = 0
      lazy = SmokeMonster::LazyCover.new(-> { LazyCoverTest.was_called += 1 })
      lazy.the_original_subject
      LazyCoverTest.was_called.must_equal 1
      lazy.the_original_subject
      LazyCoverTest.was_called.must_equal 1
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smoke_monster-0.3.3 spec/smoke_monster/lazy_cover_spec.rb
smoke_monster-0.3.2 spec/smoke_monster/lazy_cover_spec.rb