简体   繁体   中英

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM