Welcome Guest, Not a member yet? Register   Sign In
How do I mock Models when testing?
#2

Your mocking approach generally looks good to me. The issue is that you aren’t using Factories for your instantiation. Instead of `return new UserModel();` you should get the shared instance from factories, e.g. with the helper method:
return model(self::class);

Factories is intended to be a central factory manager so having UserModel::factory() is a bit redundant. I would normally handle your original call like this:
$user = model(UserModel::class)->find($userID);

If you don’t like the helper method and would prefer to be explicit about the fact that this is a factory then you can use the class directly:
Factories::model(UserModel::class)
Reply


Messages In This Thread
How do I mock Models when testing? - by tgix - 06-17-2022, 03:50 AM
RE: How do I mock Models when testing? - by MGatner - 06-17-2022, 04:05 AM
RE: How do I mock Models when testing? - by tgix - 06-17-2022, 04:19 AM
RE: How do I mock Models when testing? - by tgix - 06-19-2022, 07:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB