Basically I want to test that when I call a method 2 times another method is called once but I get the following Exception:
Mockery\Exception\BadMethodCallException : Received Mockery_0_App_Repository_DimensionRepository::getThinClientDimension(), but no expectations were specified
My test is as follows
class HostRepositoryTest extends TestCase
{
/**
* @var HostRepository
*/
private $hostRepository;
/**
* @var Dimension
*/
private $dimension;
public function setUp(): void
{
parent::setUp();
$this->dimension = new Dimension();
$mockDimensionRepository = Mockery::mock(DimensionRepository::class);
$mockDimensionRepository->shouldReceive('getDimensionByName')
->once()
->andReturn($this->dimension);
$this->hostRepository = new HostRepository($mockDimensionRepository);
}
/**
* Test lazy loading dimension retrieval
*/
public function testGetThinClientDimension()
{
$this->hostRepository->getEnvironmentsHostList([]);
$this->hostRepository->getEnvironmentsHostList([]);
}
}
HostRepository:
[...]
/**
* @param $configurationIds
* @return Host[]|Collection
*/
public function getEnvironmentsHostList($configurationIds)
{
//dd('test'); If I uncomment this it will be executed in the test
$hostDimension = $this->dimensionRepository->getThinClientDimension();
dd($hostDimension); // This is not executed if the test is ran
//Returns an entity through Eloquent ORM
[...]
}
DimensionRepositoy:
class DimensionRepository
{
private $thinClientDimension;
const THINCLIENT = 'ThinclientId';
[...]
public function getDimensionByName($name)
{
return Dimension::where(['Name' => $name])->firstOrFail();
}
/**
* Lazy load Thinclient dimension
* @return Dimension
*/
public function getThinClientDimension()
{
dd('test'); // This part is not executed when running the test which I find weird
if ($this->thinClientDimension === NULL) {
$this->thinClientDimension
= $this->getDimensionByName(self::THINCLIENT);
}
return $this->thinClientDimension;
}
[...]
Update:
It seems that when I call $this->dimensionRepository->getThinClientDimension()
(in getEnvironmentsHostList
) the exception is thrown.
Seems I have to mock this as well (getThinClientDimension
) which would make my test useless because as you can see it delegates the call to the mocked method getDimensionByName
...