我正在尝试创建一个模拟对象并将其传递给构造函数。我收到错误
Users
✘ GetUser ItShouldThrowAnExceptionIfUserDoesNotExist
┐
├ Failed asserting that exception of type "TypeError" matches expected exception "Foo\Exceptions\UserNotFoundException". Message was: "Argument 2 passed to Foo\Clients\Users::__construct() must be an instance of Foo\Api\UsersApi, instance of Mockery_0__UsersApi given, called in /Users/tomcaflisch/myproject/tests/Clients/UsersTest.php on line 25" at
├ /Users/tomcaflisch/myproject/lib/Clients/Users.php:26
├ /Users/tomcaflisch/myproject/tests/Clients/UsersTest.php:25
├ .
┴
根据the docs这应该有效。
用户类别
class Users
{
public function __construct(?string $secret, UsersApi $usersApi = null)
{
}
UsersTest类
use Mockery\Adapter\Phpunit\MockeryTestCase;
class UsersTest extends MockeryTestCase
{
public function testGetUser_ItShouldThrowAnExceptionIfUserDoesNotExist()
{
$this->expectException(UserNotFoundException::class);
$this->expectExceptionMessage("User with id, email, or username <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56303939163437247835393b" rel="noreferrer noopener nofollow">[email protected]</a> not found");
$usersApi = Mockery::mock('UsersApi');
$usersApi->shouldReceive('get')
->andThrow(new UserNotFoundException("User with id, email, or username <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89e9797b89a998ad69b9795" rel="noreferrer noopener nofollow">[email protected]</a> not found"));
$usersClient = new Users(null, $usersApi);
}
最佳答案
啊,我刚刚想通了。我需要使用完整的命名空间。
即
替换
$usersApi = Mockery::mock('UsersApi');
与
$usersApi = Mockery::mock('Foo\Api\UsersApi');
关于php - 模拟类时出现 Mockery TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62046438/