Summer Sale! All accounts are 50% off this week.

sh1r3f's avatar

how to mock a dependency injected class on a package?

this is a little bit complicated but I hope we can figure it out together. I'm building a laravel package that depends on another package. Let's say I'm building Package X that depends on Package Y in my package X I need to mock the Request injected to a constructor class that will be used in Package Y. it this possible?

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, it is possible to mock a dependency injected class on a package. You can use the Mockery package to create a mock object of the class you want to inject. Here is an example of how you can do this:

// Create a mock object of the class you want to inject
$mock = Mockery::mock('PackageY\Request');

// Set the expected behavior of the mock object
$mock->shouldReceive('get')->andReturn('foo');

// Inject the mock object into the constructor of Package X
$packageX = new PackageX($mock);

You can then use the mock object in your tests to verify that the expected behavior is being executed.

Please or to participate in this conversation.