amilajack's avatar

Dependency Injecting into Test

How would I inject dependencies that help test my application? I currently have this:

use App\Services\Faker\ListingFaker;

class ListingApiTest extends TestCase
{

protected $listingFaker;

  public function __construct(ListingFaker $listingFaker)
  {
    $this->listingFaker = $listingFaker;
  }

and I'm getting the following error:

Catchable fatal error: Argument 1 passed to ListingApiTest::__construct() must be an instance of App\Services\Faker\ListingFaker,

The reason why I cannot use a factory for this is because the functionality to create fake data cannot be done with a factory.

0 likes
2 replies
Thijmen's avatar

This cannot be done as this is PHPUnit and it's a component outside Laravel. However, you can create a new object in your setUp() function in your test.

phildawson's avatar

Like so:

public function setUp()
{
    $this->listingFaker = new ListingFaker;
}

Please or to participate in this conversation.