假设有一个方法
public function updateTimestamp($sessionId, $data)
{
return $this->memcached->touch($sessionId, time() + $this->ttl);
}
我想测试。 Infection更改
+
在 time() + $this->ttl
至 -
而且我所有的测试都还在通过。所以我想做一个测试来模拟 Memcached 对它的影响,并有条件地返回 true
/false
基于传递给它的第二个参数。所以做这样的事情:
// $ttl is set;
$memcachedMock
->touch(
'sessionId',
$certainValuePassedToTouch
)
->willReturn(
$certainValuePassedToTouch >= time() + $ttl
? true
: false
)
;
现在有两个问题:
最佳答案
也许你可以看看这个工具:
基于 PHP 的命名空间回退策略 ( https://www.php.net/manual/en/language.namespaces.fallback.php ),它允许模拟 php 原生函数,如 time()、date() 等。
关于php - 用先知 mock 一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61976480/