我无法使用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 []
答案 0 :(得分:0)
设置所需响应的正确格式为......
start_place.expect(:first_place, end_place)
如果您定义了一种方法,则无法访问该方法之外定义的变量,因此您的def start_place.first_place...
代码无效。