Answer the question
In order to leave comments, you need to log in
How to lock the results of a method so that it doesn't make an API call?
there is a situation, there is a method that I want to test, there is a method:
public function getPaidShops() : array
{
$categories = $this->getCategories();
$data = $this->getLicense($categories);
return $this->format($data);
}
private function getLicense(array $categories)
{
return app(APIService::class)->getLicenses($categories);
}
Answer the question
In order to leave comments, you need to log in
Make the getLicense method protected and for the test inherit from the main class and replace the getLicense method.
Then you won't have to rewrite anything at all.
public function testCase()
{
$mockObject = $this->createMock(NeedClass::class);
// чем это принципиально отличается от мока с точки зрения теста - непонятно
$mockObject = new class extends NeedClass {
protected function getLicense(array $categories)
{
return 'need license';
}
};
$this->assertEqual($mockObject->getPaidShops(), 'actual value');
}
Alternatively, you can rewrite the code so that APIService is passed to the class being tested in the constructor (and not created directly in the method), and in unit tests, pass the mock of this class to the constructor with the data that will be thrown into getLicenses from the service provider
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question