Object Mother

An Object Mother is a catchy name for a Factory that creates fixed fixtures for your tests.
Similar to the previous example, we could extract the duplicated logic to an Object Mother so it could be reused across tests:

class AuthorObjectMother
{
public static function createOne()
{
return new Author(
new Username('johndoe'),
new FullName('John', 'Doe'),
new Email('[email protected] )
);
}
}

class MyTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function itDoesSomething()
{
$author = AuthorObjectMother::createOne();
}
}

You'll notice that the more tests and situations you have, the more methods the Factory will have.

As Object Mothers aren't very flexible, they tend to grow in complexity quickly. Luckily, there's a more flexible alternative for your tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset