我正在尝试创建一个mock对象并将其传递给一个构造函数。我得到了一个错误
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的说法,这应该是可行的。
Users类
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 foo@bar.com not found");
$usersApi = Mockery::mock('UsersApi');
$usersApi->shouldReceive('get')
->andThrow(new UserNotFoundException("User with id, email, or username foo@bar.com not found"));
$usersClient = new Users(null, $usersApi);
}
发布于 2020-05-27 15:13:59
啊,我刚刚想通了。我需要使用完整的名称空间。
即
替换
$usersApi = Mockery::mock('UsersApi');
使用
$usersApi = Mockery::mock('Foo\Api\UsersApi');
https://stackoverflow.com/questions/62046438
复制