php - 模拟类时出现 Mockery TypeError

标签 php phpunit mockery

我正在尝试创建一个模拟对象并将其传递给构造函数。我收到错误

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/

相关文章:

php - 使用 Selenium Facebook PHP Webdriver 在 chrome 中打开新标签

php - 如何模拟 Laravel Eloquent 访问器属性

php - 没有缓存的谷歌云存储公共(public)访问

PHP - PDO 连接类 - 无法访问

php - 使用 mysql 查询来检查时间戳值

php - 如何在 Laravel 单元测试中避免 ThrottleRequestsException?

eclipse - 测试套件的 phpunit eclipse 配置。文档?

php - 我应该在登台/生产环境中使用 PHPUnit 吗?

php - 尝试模拟模型外观会返回 shouldReceive 未定义的错误

php - mock :被动部分模拟与默认模拟有何不同?