Rails 模擬測試框架 FlexMock 0.9.0 發布
FlexMock是一個比較流行的針對Rails單元測試的Mock工具,
安裝方法:$ gem install flexmock
代碼示例:
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>
項目地址:http://flexmock.rubyforge.org/
FlexMock 0.9.0 增加了使用 when() 的狀態機支持,其他方面改進還包括:
- It makes expectations fail as soon as the number of expected calls is exceeded.
- flexmock_teardown no longer returns a function.
同時還修復了一些 bug:
- fixes should_call(), allowing it on class and static methods while disallowing on class mocks.
- fixes Unicode argument handling issues.
- fixes issues with @property methods misbehaving in the debugger.
- fixes pytest integration and instance teardown.
- fixes private method handling
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!