Rails 单元测试,flexmock 0.9.3 发布
fmms 13年前
<p><a href="/misc/goto?guid=4958197824004491022" target="_blank">flexmock </a>0.9.3 发布,新版本增加了 Python 3.3 作为目标测试平台,可正常处理跨不同方法的 ordered() 期望,支持伪对象,修复了 pytest 2.2 的兼容性问题等等。</p> <p>FlexMock是一个比较流行的针对Rails单元测试的Mock工具,</p> <p>安装方法:$ gem install flexmock</p> <p>代码示例:</p> <pre> require 'test/unit' require 'flexmock/test_unit' 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 = flexmock("temp") sensor.should_receive(:read_temperature).times(3). and_return(10, 12, 14) sampler = TemperatureSampler.new(sensor) assert_equal 12, sampler.average_temp end end</pre>