我正在学习如何使用模拟来运行一些单元测试,但我不知道如何模拟我的数据库类。它由单独的方法组成,这些方法可以像这两个示例一样链接起来:
$db->select('someTblName',['fieldName'])
->where('fieldName', 'someValue')
->runQuery()
->fetch(); //returns array or null
另一种用法可能是这样的:
$db->select('someTblName')
->where('fieldName', 'someValue')
->where('fieldName', array('>=' , 'someValue')
->runQuery()
->fetch(); //returns array or null
通过阅读一些文档,我发现我可以做类似的事情:(对于第一种情况)
$db = \Mockery::mock('Database');
$db->shouldReceive('select', 'where', 'runQuery', 'fetcth')
->with(??????)
->andReturn(null);
现在我感兴趣的是如何将“相应”参数传递给方法?而且,我将如何模拟第二种情况。
你可以做shouldReceive('select->where->runQuery->fetch')
如果你不关心争论。如果您确实想检查参数,则必须执行以下操作来链接期望:
$db->shouldReceive('select')->with('someTblName', ['fieldName'])
->once()->andReturn(m::self())->getMock()
->shouldReceive('where')...
最后一个 shouldReceive 是shouldReceive('fetch')->andReturn(null)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)