【发布时间】:2021-10-11 09:20:36
【问题描述】:
我有一堂课:
namespace Models;
use Contracts\PathContract;
class Path implements PathContract
{
public static function getStorage(string $type): string
{
return storage_path(config('logs.storage_path', 'logs'))
.DIRECTORY_SEPARATOR
.$type;
}
public static function directoryExists(string $type): bool
{
var_dump(Path::getStorage($type)); // output: .../logs/vendor/orchestra/testbench-core/laravel/storage/logs/myCustomType
if (is_dir(self::getStorage($type))) {
return true;
}
return false;
}
}
我要测试directoryExists静态方法:
public function testDirectoryExists(): void
{
$path = Mockery::mock(Path::class)->makePartial();
$path->shouldReceive('getStorage')
->once()
->andReturn('/var/log');
var_dump($path::getStorage('blah blah blah')); // output: /var/log
$this->assertTrue($path::directoryExists('myCustomType'));
}
从directoryExists 方法,getStorage 方法不返回模拟值而是返回真实值。有什么想法吗?
如何测试从同一类调用另一个静态方法的静态方法?
【问题讨论】:
-
不可能模拟/更改硬连线(静态)调用的实现。你应该改用依赖注入,因为这样你可以在你的模拟类中交换。
标签: php laravel phpunit mockery