Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

php - Why console application fails to retrieve mocked service during testing?

I have the following console application:

namespace AppConsoleCommands;

use IlluminateConsoleCommand;
use AppServicesMyservice;

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

And I have the following service:

namespace AppServices;

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

I also made the following test:

namespace TestsConsole;

use IlluminateFoundationTestingTestCase;

use Mockery;

class TestMyCommand extends TestCase
{

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

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

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

}


But I retrieve the following error:

There was 1 error:

1) TestsConsoleTestMyCommand::testCommand
MockeryExceptionInvalidCountException: 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?

#Edit 1:

I tried the following:

<?php

namespace TestsConsole;

use IlluminateFoundationTestingTestCase;
use TestsCreatesApplication;
use Mockery;

class TestMyCommand extends TestCase
{
   use CreatesApplication;

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

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

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

}

And the following:

<?php

namespace TestsConsole;

use IlluminateFoundationTestingTestCase;
use TestsCreatesApplication;
use Mockery;

class TestMyCommand extends TestCase
{
   use CreatesApplication;

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

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

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

}

But still get the error:

Time: 3.32 seconds, Memory: 38.00 MB

There was 1 error:

1) TestsConsoleTestMyCommand::testCommand
MockeryExceptionInvalidCountException: 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/TestMyCommand.php:21

ERRORS!
Tests: 1, Assertions: 2, Errors: 1.

Generating code coverage report in HTML format ... done


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...