相关疑难解决方法(0)

Laravel 测试 | Laravel Artisan Command 中的模拟对象

我想测试我的 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 命令测试中模拟对象?

command unit-testing laravel laravel-artisan

5
推荐指数
1
解决办法
1317
查看次数

标签 统计

command ×1

laravel ×1

laravel-artisan ×1

unit-testing ×1