Tala Naji
Tala Naji

Reputation: 31

Laravel Mockery - This test did not perform any assertions

I am trying to create a mockery unit test but it giving me this error

This test did not perform any assertions

public function testGetById()
{
    $mock = Mockery::mock(PostService::class)->makePartial();
    $mock->shouldReceive('getById')
        ->withSomeOfArgs(1);
        $mock->getById(1);
}

! get by id→ This test did not perform any assertions \tests\Unit\PostControllerTest.php:30

Tests: 1 risked Time: 0.32s Warning: TTY mode is not supported on Windows platform.

Upvotes: 3

Views: 4130

Answers (2)

SomeOne_1
SomeOne_1

Reputation: 1002

There are two options.

Option 1:

  • make sure you have added a tearDown method with `Mockery::close();
  • then use any assertion.
  • If you don't really need one, use $this->assertTrue(true);

Option 2:

You can also extend Mockery\Adapter\Phpunit\MockeryTestCase instead of the usual PHPUnit\Framework\TestCase. That way you don't have to add phpunit assertions at all.

Upvotes: 3

matiaslauriti
matiaslauriti

Reputation: 8082

I am sorry that I cannot help you 100%, I cannot setup a testing project right now to try this, but you can try it:

public function testGetById()
{
    $mock = Mockery::spy(PostService::class);
    // Bind the mock to the Service Container so it gets injected (DI)

    // Execute the code that would run that mock
    $mock->shouldHaveReceived('getById')
        ->withSomeOfArgs(1);
}

Spies allow you to spy if the desired method got called with desired conditions. If you mock, you are just "faking" what it will return or do, if you spy, you will be able to assert what happened to a method.

Read more about it on the official Mockery documentation.

Upvotes: 2

Related Questions