我是PHPUnit的新手,正在尝试设置我的第一个测试。它失败并出现以下错误消息
无法断言ReflectionProperty Object(...)是类“Illuminate \ Database \ Eloquent \ Model”的实例。
基本上,我试图模拟我注入构造函数的模型的创建,并通过反射测试它是正确的类型。我遵循这篇文章/答案中提出的建议:
如果有人能给我一些指导,那将非常感激。
正在测试的课程在这里:
<?php
namespace PlaneSaleing\Repo\Listing;
use Illuminate\Database\Eloquent\Model;
class EloquentListing implements ListingInterface {
protected $advert;
public function __construct(Model $advert)
{
$this->advert = $advert;
}
/**
* Get paginated listings
*
* @param int Current page
* @param int Number of listings per page
* @return StdClass object with $items and $totalItems for pagination
*/
public function byPage($page=1, $limit=10)
{
$result = new \StdClass;
$result->page = $page;
$result->limit = $limit;
$result->totalItems = 0;
$result->items = array();
$listings = $this->advert
->orderBy('created_at')
->skip( $limit * ($page-1) )
->take($limit)
->get();
// Create object to return data useful for pagination
$result->items = $listings->all();
$result->totalItems = $this->totalListings();
return $result;
}
服务提供商在这里:
<?php
namespace PlaneSaleing\Repo;
use Illuminate\Support\ServiceProvider;
use PlaneSaleing\Repo\Listing\EloquentListing as Listing;
use \Advert;
class RepoServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('PlaneSaleing\Repo\Listing\ListingInterface', function($app) {
$app->make(Listing(new Advert));
} );
}
}
我的测试在这里:
<?php
use \Mockery;
use \ReflectionClass;
use PlaneSaleing\Repo\Listing\EloquentListing;
class EloquentListingTest extends \TestCase
{
/**
* Testing if __constructor is setting up property
*/
public function testModelSetsUp()
{
$mock1 = Mockery::mock(Illuminate\Database\Eloquent\Model::class);
$listing = new EloquentListing($mock1);
$reflection = new ReflectionClass($listing);
// Making your attribute accessible
$property1 = $reflection->getProperty('advert');
$property1->setAccessible(true);
$this->assertInstanceOf(Illuminate\Database\Eloquent\Model::class, $property1);
}