在我的单元测试文件中有这一行:
$this->object->drupal->shouldReceive('drupalSetMessage')->once();但我得到的错误如下:
$ ./tests/phpunit -c tests/phpunit.xml
...
There was 1 error:
1) SessionTest::testFoo
Mockery\Exception\InvalidCountException: Method drupalSetMessage(<Any Arguments>) from Mockery_1_Drupal_Util_Drupal should be called
exactly 1 times but called 0 times.
tests/vendor/mockery/mockery/library/Mockery/CountValidator/Exact.php:38
tests/vendor/mockery/mockery/library/Mockery/Expectation.php:309
tests/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:119
tests/vendor/mockery/mockery/library/Mockery/Container.php:301
tests/vendor/mockery/mockery/library/Mockery/Container.php:286
tests/vendor/mockery/mockery/library/Mockery.php:165如何调试才能看到在测试过程中使用了哪些参数调用了方法?
发布于 2018-02-22 21:47:21
我发现的变通方法是在Mock.php中修改_call()/_callStatic。
例如,我编辑了文件:vendor/mockery/mockery/library/Mockery/Mock.php。
然后我添加了这一行:
if ($method == 'drupalSetMessage') var_dump($method . " " . var_export($args, TRUE));所以函数看起来像这样:
public function __call($method, array $args)
{
if ($method == 'drupalSetMessage') var_dump($method . " " . var_export($args, TRUE));
return $this->_mockery_handleMethodCall($method, $args);
}
public static function __callStatic($method, array $args)
{
if ($method == 'someStaticMethodName') var_dump($method, $args);
return self::_mockery_handleStaticMethodCall($method, $args);
}下面是分散注意力的日志记录行(它可以处理circular references):
syslog(LOG_DEBUG, sprintf("%s: %s(%s)", __METHOD__, $method, json_encode($args, TRUE)));然后可以在syslog中找到输出。在macOS上,可以通过以下命令显示日志流:
log stream --level debug --predicate 'processImagePath contains "php"要打印发生函数的列表(回溯),下面这一行可能很有用:
printf("%s(%s) at %s\n", $method, json_encode($args, TRUE), implode(', ', array_column(debug_backtrace(), 'function')));https://stackoverflow.com/questions/48929029
复制相似问题