lundi 11 janvier 2021

Why console application fails to retrieve mocked service during testing?

I have the following console application:

namespace App\Console\Commands;

use Illuminate\Console\Command;


class MyCommand extends Console
{
    protected $signature = 'health';
    
    public function handle(Myservice $service){
       dump($service->dummy());
    }
}

And I have the following service:

namespace App\Services;

class Myservice
{
   public function dummy()
   {
      return false;
   }
}

I also made the following test:

namespace Tests\Console;

use Illuminate\Foundation\Testing\TestCase;

use Mockery;

use TestMyCommand extends TestCase
{

   public function testCommand()
   {
       $service = Mockery::mock(Myservice::class);
       $service->shouldReceive('dummy')->andReturn(true);

       app()->bind(MyService::class,$service);

       $this->artisan('test')
       $soapService->shouldHaveReceived('dummy')->andReturn(true);
   }

}


But I retrieve the following error:

There was 1 error:

1) Tests\Console\TestMyCommand::testCommand
Mockery\Exception\InvalidCountException: Method dummy(<Any Arguments>) from Mockery_0__Tests_Console_MyService should be called
 at least 1 times but called 0 times.

/var/www/html/api/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/api/vendor/mockery/mockery/library/Mockery/Expectation.php:312
/var/www/html/api/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/api/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/api/tests/Console/MyCommand.php:20

So why I am unable to provide a mocked service to the command?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3oB2wNd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire