FlexMockRails模拟测试工具

联合创作 · 2023-10-01 16:14

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
浏览 6
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报