为啥我会通过 Mockery 获得 NoMatchingExpectationException?

Posted

技术标签:

【中文标题】为啥我会通过 Mockery 获得 NoMatchingExpectationException?【英文标题】:Why do I get NoMatchingExpectationException with Mockery?为什么我会通过 Mockery 获得 NoMatchingExpectationException? 【发布时间】:2017-04-30 04:42:43 【问题描述】:

我正在尝试为我的 Web 应用程序编写一些单元测试。我使用 Laravel 5.3 和 Mockery。

这是简化的代码:

class UserService

    protected $userRepository;

    public function __construct(IUserRepository $userRepository)
    
        $this->userRepository = $userRepository;
    

    public function saveUserWithGeneratedPassword($user, $companyId, $isAdmin)
    
        try 
            $user['password'] = $this->encryptUserPassword($this->createPassword());
            return $this->userRepository->saveUser($user, $companyId, $isAdmin);
         catch (\Exception $e) 
            Log::error(
                'Exception occured while trying to create new user: ' . $e->__toString()
            );
            return false;
        
    


class UserRepository implements IUserRepository

    /**
     * @var User
     */
    private $model;

    /**
     * UserRepository constructor.
     *
     * @param User $model
     */
    public function __construct(User $model)
    
        $this->model = $model;
    

    /**
     * @param $user
     * @param $companyId
     * @param $isAdmin
     * @return static
     */
    public function saveUser($user, $companyId, $isAdmin)
    
        $user = $this->model->create($user);
        $user->companies()
            ->attach($companyId, ['is_admin' => $isAdmin]);

        return $user;
    

这是测试代码:

class UserPasswordResetTest extends TestCase 
    private $userService;

    public function setUp()
    

        parent::setUp();

        $this->user = Mockery::mock('Eloquent', User::class)
            ->makePartial();

        $this->userRepository = Mockery::mock(IUserRepository::class);
        $this->app->instance(IUserRepository::class, $this->userRepository);

        $this->userRepository->shouldReceive('saveUser')
            ->withArgs(
                [
                    'userData',
                    'companyId',
                    'isAdmin'
                ]
            )
            ->andReturn($this->user);


        $this->userService = new UserService($this->userRepository, $this->passwordResetRepository);
    

    public function tearDown()
    
        parent::tearDown();
        Mockery::close();
    

    public function testSaveUserWithGeneratedPassword()
    
        $userData = [
            'first_name' => 'firstname',
            'last_name'  => 'lastname',
            'phone'      => '0123456789',
            'mobile'     => '0123456789',
            'locale'     => 'en',
            'email'      => 'best_email@ever.com',
            'is_active'  => '1',
            'password'   => 'password'
        ];

        $companyId = "1";
        $isAdmin = "0";


        $result = $this->userService->saveUserWithGeneratedPassword($userData, $companyId, $isAdmin);
        $this->assertInstanceOf(User::class, $result);
        $this->assertInternalType("string", $result->password);
    

这是个例外:

Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_... Either the method was unexpected or its arguments matched no expected argument list for this method

Stack trace:
0 /home/vagrant/Code/colo21-core/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(16) : eval()'d code(683): Mockery\ExpectationDirector->call(Array)
1 /home/vagrant/Code/colo21-core/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(16) : eval()'d code(761): Mockery_3_App_Contracts_IUserRepository->_mockery_handleMethodCall('saveUser', Array)
2 /home/vagrant/Code/colo21-core/app/Services/UserService.php(122): Mockery_3_App_Contracts_IUserRepository->saveUser(Array, '1', '0')
3 /home/vagrant/Code/colo21-core/tests/UserPasswordResetTest.php(116): App\Services\UserService->saveUserWithGeneratedPassword(Array, '1', '0')
4 [internal function]: UserPasswordResetTest->testSaveUserWithGeneratedPassword()
5 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/Framework/TestCase.php(1103): ReflectionMethod->invokeArgs(Object(UserPasswordResetTest), Array)
6 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/Framework/TestCase.php(954): PHPUnit_Framework_TestCase->runTest()
7 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/Framework/TestResult.php(701): PHPUnit_Framework_TestCase->runBare()
8 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/Framework/TestCase.php(909): PHPUnit_Framework_TestResult->run(Object(UserPasswordResetTest))
9 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/Framework/TestSuite.php(728): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
10 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(487): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
11 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
12 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/src/TextUI/Command.php(118): PHPUnit_TextUI_Command->run(Array, true)
13 /home/vagrant/Code/colo21-core/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()
14 main

编辑

最后还是不行。我发现在用户模拟上没有正确设置密码属性。我删除了第二个断言并在 UserService 上注释掉了这一行:

$user['password'] = $this->encryptUserPassword($this->createPassword());

之后它就可以工作了。当我删除注释行时,它再次失败。但是我也不能嘲笑它,因为它是 SUT。

这是来自测试的当前代码sn-p:

public function testSaveUserWithGeneratedPassword()


    $companyId = '1';
    $isAdmin = '0';
    $email = 'best_email@ever.com';

    $userData = [
        'first_name' => 'firstname',
        'last_name'  => 'lastname',
        'phone'      => '0123456789',
        'mobile'     => '0123456789',
        'locale'     => 'en',
        'email'      => $email,
        'is_active'  => '1',
    ];

//        $this->user->shouldReceive('getAttribute')
//        ->with('password')
//        ->andReturn('$2y$10$2jZTKnkmYsF8rr3wqIy.m.e06p9wtIukIOv10gMSXKQ6T7ogkmECW');

    $this->userRepository->shouldReceive('saveUser')
        ->withArgs(
            [
                $userData,
                $companyId,
                $isAdmin
            ]
        )
        ->andReturn($this->user);

    $result = $this->userService->saveUserWithGeneratedPassword($userData, $companyId, $isAdmin);
    $this->assertInstanceOf(User::class, $result);

【问题讨论】:

【参考方案1】:

您使用以下参数定义对 saveUser 的预期调用:

->withArgs(
            [
                'userData',
                'companyId',
                'isAdmin'
            ]
        )

但它实际上是用这些来调用的:

Mockery_3_App_Contracts_IUserRepository->saveUser(Array, '1', '0')

所以它不匹配。 您不必将参数的名称放在那里,而是将它们的值放在那里。如果您需要通配符,请尝试\Mockery::any()

进一步阅读:http://docs.mockery.io/en/latest/reference/argument_validation.html

【讨论】:

感谢您的意见。我想我也尝试将参数类型与抛出的相同异常相匹配。但是我明天会重试。再次感谢! 最后还是不行。我发现密码属性没有在用户模拟上正确设置。这是当前的代码 sn -p:【参考方案2】:

经过进一步的修补,我想通了。 问题是为用户数组动态生成的密码。 userRepository 模拟除了在 with() 函数中定义的确切参数。我从测试范围中删除了该函数,现在它可以工作了,因为模拟得到的值与定义的完全相同。

【讨论】:

以上是关于为啥我会通过 Mockery 获得 NoMatchingExpectationException?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Mockery 无法为我的模型创建别名

为啥从子组件返回时我会从查询结果中获得空指针?

为啥我会在这里获得 SIGABRT?

为啥我会获得额外的文本节点作为根节点的子节点?

为啥我会从 VBA Select Case 获得随机/错误的输出?

为啥我会为这个最简单的 NSAsynchronousFetchRequest 用例获得 Multithreading_Violation_AllThatIsLeftToUsIsHonor?