Minitest:不再期待

时间:2016-09-12 09:00:19

标签: ruby minitest

我使用minitest为我的web项目编写测试 我有一个模拟对象,模拟了3个方法。该对象表示具有多个属性的数据库实体(hanami模型)。现在,如果我打电话给" getter"对于一个属性不止一次,我得到一个MockExpectationError:没有更多的期望可用于属性 如何在不创建保存返回数据的变量的情况下处理此问题? 以下是我设置此方法的方法:

user_mock = Minitest::Mock.new
user_mock.expect :is_allowed?, true, [String, String]
user_mock.expect :ad_login, 'somestring'
user_mock.expect :id, 2

1 个答案:

答案 0 :(得分:1)

只需在您多次调用的函数中添加几个expects

说它是

user_mock.expect :is_allowed?, true, [String, String]

您可以添加更多预期,甚至可以更改每次调用中的返回值或预期参数。 因此,如果您想要调用它3次,只需再添加2次,这样您最终会得到:

user_mock = Minitest::Mock.new
user_mock.expect :is_allowed?, true, [String, String]
user_mock.expect :is_allowed?, false, [String, String]
user_mock.expect :is_allowed?, true, [String, String]
user_mock.expect :ad_login, 'somestring'
user_mock.expect :id, 2

将按照您定义的顺序返回值。