Rails 模拟测试框架 FlexMock 0.9.0 发布
openkk 13年前
<p>FlexMock是一个比较流行的针对Rails单元测试的Mock工具,</p> <p>安装方法:$ gem install flexmock<br /> <br /> 代码示例:<br /> </p> <pre class="brush:ruby; toolbar: true; auto-links: false;"> 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> <p></p> <p><span style="font-weight:bold;">项目地址:</span><a style="font-weight:bold;" href="/misc/goto?guid=4958197824004491022" target="_blank">http://flexmock.rubyforge.org/</a><br /> <br /> FlexMock 0.9.0 增加了使用 when() 的状态机支持,其他方面改进还包括:</p> <ul> <li>It makes expectations fail as soon as the number of expected calls is exceeded. </li> <li>flexmock_teardown no longer returns<span class="truncate_more"> a function. <br /> </span></li> </ul> <p>同时还修复了一些 bug:</p> <ul> <li><span class="truncate_more">fixes should_call(), allowing it on class and static methods while disallowing on class mocks. <br /> </span></li> <li><span class="truncate_more">fixes Unicode argument handling issues. <br /> </span></li> <li><span class="truncate_more">fixes issues with @property methods misbehaving in the debugger. <br /> </span></li> <li><span class="truncate_more">fixes pytest integration and instance teardown. <br /> </span></li> <li><span class="truncate_more">fixes private method handling<br /> </span></li> </ul>