我正在使用通过GitHub API与Laravel API包装器。我创建了一个依赖注入类。如何模拟
App\Http\GitHub.php
类中的exists
方法?
App\Http\GitHub.php
:use GrahamCampbell\GitHub\GitHubManager;
class Github
{
public $username;
public $repository;
public function __construct($username, $repository, GitHubManager $github)
{
$this->username = $username;
$this->repository = $repository;
$this->github = $github;
}
public static function make($username, $repository)
{
return new static($username, $repository, app(GitHubManager::class));
}
/**
* Checks that a given path exists in a repository.
*
* @param string $path
* @return bool
*/
public function exists($path)
{
return $this->github->repository()->contents()->exists($this->username, $this->repository, $path);
}
}
测试:
use App\Http\GitHub;
public function test_it_can_check_if_github_file_exists()
{
$m = Mockery::mock(GitHub::class);
$m->shouldReceive('exists')->andReturn(true);
app()->instance(GitHub::class, $m);
$github = GitHub::make('foo', 'bar');
$this->assertTrue($github->exists('composer.lock'));
}
运行这个测试实际上会调用API,而不仅仅是返回模拟的true
值,我在这里做错了什么?