Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created November 12, 2010 10:34
Show Gist options
  • Select an option

  • Save floehopper/673960 to your computer and use it in GitHub Desktop.

Select an option

Save floehopper/673960 to your computer and use it in GitHub Desktop.
require "test/unit"
require "rubygems"
require "mocha"
class Bar
def bar; end
end
def foo
timer = Timer.start(60)
result = Bar.bar
timer.expired? ? "FAILED" : result
end
class Timer
def self.start(seconds)
timer = new(seconds)
timer.start
end
def initialize(seconds)
@seconds = seconds
end
def start
@start_time = Time.now
end
def expired?
Time.now - @start_time < @seconds
end
end
class FooTest < Test::Unit::TestCase
def test_returns_success_when_timer_not_expired
Timer.stubs(:start).returns(stub("timer", :expired? => false))
Bar.stubs(:bar).returns("SUCCESS")
assert_equal "SUCCESS", foo
end
def test_returns_failed_when_timer_expired
Timer.stubs(:start).returns(stub("timer", :expired? => true))
Bar.stubs(:bar).returns("SUCCESS")
assert_equal "FAILED", foo
end
end
@floehopper

Copy link
Copy Markdown
Author

@pathsny

pathsny commented Nov 12, 2010

Copy link
Copy Markdown

oh I realise I could work around it, but the timer class feels contrived for a single use case. I was just thinking that return taking a block provides a hook for convenient actions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment