模拟函数未被调用

时间:2017-06-15 08:20:11

标签: php phpunit lumen mockery

在模拟界面方法时,我收到以下错误。

注 -

我正在使用服务存储库模式,并且基于我已经创建了一个服务,并且我正在调用我正在执行数据库操作的存储库。

  • 流明版 - 5.4
  • PhpUnit版本 - 4.8.35

错误 -

E:\event>phpunit
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.

..E

Time: 4.63 seconds, Memory: 7.25MB

There was 1 error:

1) EventServiceTest::testEventListing
Mockery\Exception\InvalidCountException: Method getEventList() from Mockery_0_App_Repositories_Event_EventInterface should be called                                                                                                       exactly 1 times but called 0 times.

E:\event\vendor\mockery\mockery\library\Mockery\CountValidator\Exact.php:37
E:\event\vendor\mockery\mockery\library\Mockery\Expectation.php:298
E:\event\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:120
E:\event\vendor\mockery\mockery\library\Mockery\Container.php:297
E:\event\vendor\mockery\mockery\library\Mockery\Container.php:282                                       
E:\event\vendor\mockery\mockery\library\Mockery.php:152
E:\event\vendor\laravel\lumen-framework\src\Testing\TestCase.php:107
E:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
E:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
FAILURES!
Tests: 3, Assertions: 2, Errors: 1.

代码 -

  public function testEventListing()
        {
            $data = [
                'startDate' => '2017-06-14 00:00:00',
                'endDate'   => '2017-06-14 23:59:59'
                ];

            $reposneData = array(
                array(
                    'eventDate' => "2017-06-14 08:00:00"
                ),
                'status' => 1
            );

            //Mocking the event Repository
            $eventRepoMock = \Mockery::mock ( App\Repositories\Event\EventInterface::class );

            $eventRepoMock->shouldReceive ( 'getEventList' )
            ->once ()
            ->with ( $eventData )
            ->andReturn ( $reposneData );

            $eventService = new EventService($eventRepoMock);

            //Fetching mocked data
            $eventObj = $eventService->geteventList( $eventData );

            //Asserting based on success result.
            $this->assertEquals(1, $eventObj['status']);
        }

1 个答案:

答案 0 :(得分:0)

I solved the issue. I was converting the collection object to array using toArray() function in my service. The mocked repository was expecting the collection but i was supplying the array. So I mocked the collection too and everything is working fine now.