Created
November 12, 2010 10:34
-
-
Save floehopper/673960 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)