我試圖嘲笑 laravel Log。這是我的代碼:
public function test_process_verify_card()
{
Log::shouldReceive('error')->once();
Log::makePartial();
$class = new MyClass();
$class->myFunction();
}
這MyClass看起來像:
class MyClass
{
public function __construct()
{
$this->logger = Logg::channel('test');
}
public function myFunction()
{
// ... some logic
$this->loggger->error('Errror!!');
}
}
當我運行測驗這個測驗用例時,它會拋出錯誤
Call to a member function runningUnitTests() on null
at vendor/laravel/framework/src/Illuminate/Log/LogManager.php:568
我試圖通過上課dd()來除錯這個錯誤LogManager
protected function parseDriver($driver)
{
$driver ??= $this->getDefaultDriver();
dd($this->app); // <--- This is my code
if ($this->app->runningUnitTests()) {
$driver ??= 'null';
}
return $driver;
}
但它表明$this->app不是null。
我之前嘗試過模擬外觀Date,效果很好。
我想測驗myFunction執行日志記錄操作。這是正確的方法嗎?
更新
我也嘗試通過partialMock()函式來??模擬它:
public function test_process_verify_card()
{
$this->partialMock(Logger::class, function (MockInterface $mock) {
$mock->shouldReceive('error')->once();
});
$class = new MyClass();
$class->myFunction();
}
但它仍然不起作用,它顯示錯誤:
Method error(<Any Arguments>) from Mockery_0_Illuminate_Log_Logger should be called
exactly 1 times but called 0 times.
at vendor/phpunit/phpunit/phpunit:98
uj5u.com熱心網友回復:
我相信為什么這不起作用的問題是Log::channel在部分模擬上回傳一個頻道。因此,模擬實體永遠不會收到錯誤呼叫。
通過在Mockery呼叫中使用“->”,您可以輕松地進行鏈接呼叫shouldReceive()。
Log::shouldReceive('channel->error')
->once()
->andReturn(null);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/516723.html
標籤:php拉拉维尔测试嘲弄
上一篇:有沒有辦法在德爾福聯合單位?
