cartoonclouds's avatar

Mocking Static Methods on Service Providers

If I were to use DI to get an instance of a Service Provider (in a Controller), but then call static methods on it, can those be intercepted when mocked in unit tests? My guess is still no.

I'm sure a simple question, I'm just not in a position to test this out right now.

Thanks.

0 likes
2 replies
puklipo's avatar

Show us your code.

In general, avoid static methods.

If you're having trouble testing, you're using static methods incorrectly.

aleahy's avatar

In your test, you're replacing the injected class instance with the mock, so yes, it would definitely work.

Calling static methods on an instance of a class does raise a few questions though. You don't need dependency injection if you are just calling static methods.

Please or to participate in this conversation.