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
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
FlexMock 的下载地址:请点这里
