模拟函数没有被调用
Mocked function not getting called
在模拟接口方法时出现以下错误。
注-
我正在使用服务存储库模式,并基于它创建了一个服务,并从中调用了一个存储库,我在其中执行数据库操作。
- Lumen 版本 - 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']);
}
我解决了这个问题。我在我的服务中使用 toArray() 函数将集合对象转换为数组。模拟存储库期待收集,但我提供了数组。所以我也嘲笑了这个系列,现在一切正常。
在模拟接口方法时出现以下错误。
注-
我正在使用服务存储库模式,并基于它创建了一个服务,并从中调用了一个存储库,我在其中执行数据库操作。
- Lumen 版本 - 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']);
}
我解决了这个问题。我在我的服务中使用 toArray() 函数将集合对象转换为数组。模拟存储库期待收集,但我提供了数组。所以我也嘲笑了这个系列,现在一切正常。