die*_*ego 5 phpunit mocking laravel laravel-6
index()我尝试测试控制器的方法。在这个方法中,有一个模型。
class UserController extends Controller
{
public function index()
{
return User::all();
}
}
Run Code Online (Sandbox Code Playgroud)
在测试课上,我有以下内容。
class UserControllerTest extends TestCase
{
public function testIndex():void
{
$user = factory(User::class)->make();
$mock = Mockery::mock(User::class);
$mock->shouldReceive('all')->andReturn($user);
$this->app->instance('User', $mock);
$response = $this->json('GET', 'api/users');
dd($response->getContent()); // error : [2002] Connection refused
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,与数据库的连接出现错误。这很奇怪,因为我模拟了该模型,这意味着我不需要建立与数据库的连接。我该如何解决这个错误?
错误
SQLSTATE[HY000] [2002] 连接被拒绝(SQL:select * from
userswhereusers.deleted_at为空)
您正在尝试使用模拟对对象实例的函数调用的方法来模拟静态调用。模拟静态函数调用并不简单,可以通过别名来完成,但不建议这样做。
一种简单的方法是将逻辑简单地包装在服务中并模拟它。
class UserService {
public function all(): Collection {
return User::all();
}
}
Run Code Online (Sandbox Code Playgroud)
现在您的模拟代码应该如下所示。
$user = factory(User::class)->make();
$mock = Mockery::mock(UserService::class);
// Teoretically all method will return Eloquent Collection, but should be fine
$mock->shouldReceive('all')->andReturn(collect($user));
$this->app->instance(UserService::class, $mock);
Run Code Online (Sandbox Code Playgroud)
当使用containerand 替换实例时,非常依赖于您使用 thecontainer而不是new关键字来获取这些模拟类。因此控制器应该看起来与此类似。
class UserController extends Controller
{
/** @var UserService **/
private $userService;
public function __construct(UserService $userService) {
// load userService from the container as the mocked instance on tests
$this->userService = $userService;
}
public function index()
{
return $this->userService->all();
}
}
Run Code Online (Sandbox Code Playgroud)
最后一点,我在多个项目中都是测试、代码覆盖率等的主要驱动力。通过在那里使用数据库来创建测试会更容易,可以使用数据库,sqlite也可以docker让环境为您提供数据库。没有测试就比提供任何重要的价值更像是一个障碍。速度在您的测试方法中至关重要,因为会有很多测试,最好快速完成,然后由于时间压力而跳过它,并且模拟所有数据库调用会很困难。
| 归档时间: |
|
| 查看次数: |
3659 次 |
| 最近记录: |