Simplifying work with custom stubs in Laravel
– blog.genijaho.dev - submitted by Geni Jaho
Stubs are fake implementations of interfaces or classes that simulate the behavior of real services.
Read more [blog.genijaho.dev]
Posts tagged with mocking
– blog.genijaho.dev - submitted by Geni Jaho
Stubs are fake implementations of interfaces or classes that simulate the behavior of real services.
Read more [blog.genijaho.dev]
Tomas shares his approach to get the most out of mocks, drop dead code, and make tests more valuable.
Read more [tomasvotruba.com]
Join 9,500+ smart developers
Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
– doeken.org - submitted by Doeke Norg
It's time to replace your mocked event dispatchers with a real one.
Read more [doeken.org]
Mocking, faking; these might sound like intimidating words if you don't know what they are about, but once you do, you'll be able to improve your testing skills significantly.
Part of "the art of testing" is being able to test code in some level of isolation to make sure a test suite is trustworthy and versatile. These topics are so important that we actually made five or six videos on them in our Testing Laravel course.
In this post, I want to share three ways how you can deal with mocking and faking. Let's dive in!
Native PHP functionality and having a good design in the first place provide everything you need to avoid a mocking library.
Read more [peakd.com]
Dave Marshall replies to a blogpost that Frank De Jonge published recently.
Read more [davedevelopment.co.uk]
⚗️ One of the most common integrations developers struggle to test is Guzzle. Fortunately, it has a `MockHandler` you can set up to return predefined responses.
— Jason McCreary (@gonedark) 23 december 2019
Since it uses the existing `Client` and `Response` objects, there's no need to change your implementation. pic.twitter.com/Ui2Zyqj2Lt
Read more [twitter.com]
Regarding https://t.co/Ky9pICMocc, here's an example (not saying it makes sense to write this test, just showing how it works). pic.twitter.com/1MOFXk1H79
— Matthias Noback (@matthiasnoback) 2 oktober 2019
Read more [twitter.com]
In an application I was working on I wanted to implement automated tweets. Of course, this logic should also be tested. In this blogpost I'd like to show you how you can easily handcraft your own mocks. Setting things up Let's first take a look at how you can tweet something in PHP. We're going to…
In a new post on her blog Jessica Mauerhan has some good examples on the various types of test doubles.
Did you know that a Mock is only one type of a test double? Most of us use the word “mock” to mean any kind of test double, but there’s actually five different types. It really can help you understand what you’re trying to accomplish with your test if you know a little bit more what you’re doing with your test doubles, so this article will explain the kinds of test doubles, when you use them, how you use them and why.
Read more [jmauerhan.wordpress.com]
Need to mock a specific method in a facade, leaving the remainder free to respond to calls normally? It's possible with "Partial Mocks". pic.twitter.com/VgVMdnBDid
— Raul (@rcubitto) October 4, 2017
Read more [twitter.com]
Matthieu Napoli, author of PHP-DI and externals.io, wrote a post on how you can use anonymous classes to quickly create mocks, spies and fixtures.
Anonymous classes were added to PHP 7. This article intends to show how useful they can be for writing tests.
Patchwork is a PHP library that allows you to change user defined functions at runtime. This can be incredibly handy when testing. I'm by no means a Ruby expert but I heard that in the Ruby world they do this kind of stuff all the time during testing.
Here's a simple example of what Patchwork can do:
function myCounter($array)
{
return count($array);
}
myCounter([1, 2]); // returns '2';
//now let's change the function
Patchwork\redefine('myCounter', function($array)
{
return count($array) ? 'a lot of items' : 'empty';
});
myCounter([1, 2]); // returns 'a lot of items";
Read the implementation docs to learn how this works behind the scenes.
Adam Wathan posted another excellent article on testing on his blog. This time he talks about creating test doubles. Adam demonstrates that creating your own fake can result in a much more readable test than using mocks or spies.
You'll learn to create an InMemoryMailer like this:
public function test_new_users_are_sent_a_welcome_email()
{
$mailer = new InMemoryMailer;
Mail::swap($mailer);
$this->post('register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'secret',
]);
$this->assertTrue($mailer->hasMessageFor('john@example.com'));
$this->assertTrue($mailer->hasMessageWithSubject('Welcome to my app!'));
}
That seems like a pretty readable test to me. Read (or view) the whole tutorial here: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/