تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Downsides of Mocks0:00

In the last episode, you got your first taste of mocks, and these can absolutely be useful, but we should also take some time to discuss the downsides. So let's think about it. This test delivers a Receipt, which is somewhat shorthand for, when a Subscription is created, a Receipt should be delivered to the User. That's really the behavior that we're trying to test here. But actually, we're doing quite a bit more. So in situations like this, always think of knowledge. What kind of knowledge does this test have, all the way down? For example, this test knows that a Subscription class is created with two arguments, a Gateway

Tests Know Too Much0:34

What kind of knowledge does this test have, all the way down? For example, this test knows that a Subscription class is created with two arguments, a gateway and a mailer. It also knows that Subscription exposes a create method. Further, it knows that the create method will accept a user. Next, it knows how this method is implemented. For example, it knows that the mailer will call a deliver method exactly one time with the following argument body. Next, it knows we have this gateway here, and it doesn't require that a create method is called, because remember, this is simply a stub.

Tight Coupling to Implementation1:08

Next, it knows we have this gateway here, and it doesn't require that a create method is called, because remember, this is simply a stub. But usually, they go hand in hand. It still knows, all right, there's probably a create method on the gateway being called, and when you do make that call, I'm going to return receiptStub. That's a lot of knowledge here, isn't it? Or another way to think of it is the test and the implementation are now linked. For example, let's switch over to subscription. Is there another way, for example, right here, would there be another way to send this welcomeEmail?

Refactors Can Break Tests2:06

And think about it. If you made that change, would it still be true that when a Subscription is created, a Receipt is delivered to the User? Is the result of that behavior the same? And the answer is, yes, you're still getting a Receipt. It works. And yet, the way our test is written, it would fail. For example, again, we don't have a Laravel app here, but I will simply var_dump, dispatch an event to handle the mail sending. Okay, this represents all of that logic.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟