Rails 單元測試,FlexMock 0.9.4 發布
FlexMock是一個比較流行的針對Rails單元測試的Mock工具,
安裝方法:$ gem install flexmock
FlexMock 0.9.4 發布,該版本主要改進內容包括:
1. This release adds support for stubbing return values on getter properties and non-callable attributes.
2. It also adds custom matcher object support to with_args(), stricter function signature checks, support for chained attributes, and iter support to Mock objects.
3. should_call() is fixed to work with class mocks, and_return() is fixed to return None by default, and partial mocks created using the func=return_value style will now use replace_with() instead of should_receive() for callable values.
4. As of this release, flexmock is also tested as working on PyPy and Jython
代碼示例:
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
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!