【问题标题】:Testing objects that reference other objects in minitest & ruby在 minitest & ruby​​ 中测试引用其他对象的对象
【发布时间】:2018-07-28 09:44:53
【问题描述】:

我在使用 minitest 测试与此类似的方法时遇到问题,其中对象引用了其他对象:

def drive num
    old_place = @current_place.name
    if num == 0
        road = @current_place.first_place_road
        @current_place = @current_place.first_place
    else
        road = @current_place.second_place_road
        @current_place = @current_place.second_place
    end
    print_drive(old_place, road)
end

我正在尝试通过创建 2 个模拟对象进行测试,并将它们的方法存根以返回另一个模拟对象。

def test_drive_to_first_place

    start_place = Minitest::Mock.new
    end_place = Minitest::Mock.new

    def start_place.name; "first place"; end
    def start_place.first_place; end_place; end
    def start_place.first_place_road; "road"; end

    def end_place.name; "end place"; end
    def end_place.first_place; nil; end
    def start_place.first_place_road; nil; end


    driver = Driver::new "driver", start_place

    driver.drive(0)

    assert_output(stdout = ......
end

我收到此错误,我不知道如何处理它。正在测试的对象没有任何属性或方法:end_place,它只是用作测试中的名称。

1) Error:
DriverTest#test_drive_to_first_place:
NoMethodError: unmocked method :end_place, expected one of []

【问题讨论】:

    标签: ruby oop reference minitest


    【解决方案1】:

    设置所需响应的正确格式是 ...

    start_place.expect(:first_place, end_place)
    

    如果你定义了一个方法,你就不能访问在方法之外定义的变量,所以你的def start_place.first_place...代码不能工作。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      相关资源
      最近更新 更多