Here's a simplified version of what I'm trying to do.
class Something{
function something(){
foreach(User::all() as $user){
$user->method();
}
}
}
And I want to spy if that method was called when something method on this class was called, but I can't mock User class as eloquent doesn't seem to be using it at all!
method itself is tested in a unit test and here I just want to make sure that method is called a number of times that's all.
@vincent15000 I should have mentioned that this query isn't a part of test, it is in a class somewhere on its own so I can't really do these kind of tricks.
@MohamedTammam There is a huge amount of logic in that method which I test it on its own for a single user that's why here in this loop I'm just trying to check if that method is called.
@stevemoretz Perhaps another way is to store the counter in the session and retrieve it when you need to read it. You can do that from inside the method itself.
I'm no expert on this, but I expect you would mock the User model and count how many times the method is called. For the purpose of this test, you don't need to actually run the method. As you say, you have already unit tested the method.
@Snapey Thanks mocking the user model was the first thing I tried but that doesn't really work since eloquent still returned real instances instead of mocked ones, it's not using the service container I think here's a simple example: