Why do I get NoMatchingExpectationException with Mockery?

3k views Asked by At

I am trying to write some unit tests for my web application. I use Laravel 5.3 and Mockery.

This is the simplified code:

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;
    }
}

This is the Testcode:

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'      => '[email protected]',
            '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);
    }
}

This is the exception:

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}

Edit

In the end it did not work. I found out that the password property was not properly set on the user mock. I removed the second assertion and commented out this line on the UserService:

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

After that it works. When I remove the commented line, it fails again. However I also can't mock it since it is the SUT.

This is the current code snippet from the test:

public function testSaveUserWithGeneratedPassword()
{

    $companyId = '1';
    $isAdmin = '0';
    $email = '[email protected]';

    $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);
}
2

There are 2 answers

2
LorenzSchaef On

You define the expected call to saveUser with these arguments:

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

But it is actually called with these:

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

So it doesn't match. You don't have to put the names of the arguments in there, but their values. If you need a wildcard, try \Mockery::any()

For further reading: http://docs.mockery.io/en/latest/reference/argument_validation.html

0
schwarz1603 On

After further tinkering i figured it out. The issue was the dynamically generated password for the user array. The userRepository mock excepts the exact parameters which were defined in the with() function. I removed the function out of the scope of the test and now it is working, since the mock gets the exact same values as defined.