Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created July 29, 2008 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save practicingruby/3165 to your computer and use it in GitHub Desktop.
Save practicingruby/3165 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'rubygems'
require 'mocha'
class TemperatureSampler
def initialize(sensor)
@sensor = sensor
end
def average_temp
total = (0...3).collect {
@sensor.read_temperature
}.inject { |i, s| i + s }
total / 3.0
end
end
class TestTemperatureSampler < Test::Unit::TestCase
def test_sensor_can_average_three_temperature_readings
sensor = mock()
sensor.expects(:read_temperature).returns(10)#.times(3).
#and_return(10, 12, 14)
sampler = TemperatureSampler.new(sensor)
#assert_equal 12, sampler.average_temp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment