我想测试我的 Laravel Artisan 命令。所以我需要模拟一个对象并存根这个模拟对象的方法。在我的测试中,我无法使用真实的SFTP环境。
这是handle()我的命令:
public function handle()
{
$sftp = new SFTP('my.sftpenv.com');
$sftp->login('foo', 'bar');
}
Run Code Online (Sandbox Code Playgroud)
我想在测试中模拟 SFTP:
$sftp = $this->createMock(SFTP::class);
$sftp->expects($this->any())->method('login')->with('foo', 'bar');
$this->artisan('import:foo');
Run Code Online (Sandbox Code Playgroud)
运行测试结果在 a 中Cannot connect to ...:22,它来自login的原始方法SFTP。所以mock/stub不生效。
所以我的问题是:如何在 Laravel Artisan 命令测试中模拟对象?