- Write the test, then write the code to satisfy that test.
- Write the complete minimum to satisfy the test
- If the class doesn't exist, only create the class.
- Always write the tests before beginning the code.
- Start by specifying the application functions.
Refactor -> write failing acceptance test ->
Before the routes are created, we start by writing our first test.
Growing Object Oriented Software - Friedman
"Programming by wishful thinking"
"A Walking Skeleton" - bare minimum code
Maybe use in-memory sqlite server.
Don't skip ahead and write code that we think we'll need. Literally follow the errors one by one.
Acceptance test until you have to write unit tests.
test/features/viewAnotherUsersTweetsTest.php
<?php
class ViewAnotherUsersTweetsTest extends TestCase
{
use DatabaseMigrations;
public function test_can_view_another_users_tweets()
{
$user = factory(User::class)->create([
'username' => 'johndoe'
]);
$tweet = factory(Tweet::class)->create([
'body' => 'My first tweet'
]);
$user->tweets()->save($tweet);
$this->visit('/johndoe')
->see('My first tweet');
}
}
test/features/UserTest.php
<?php
class UserTest extends TestCase
{
public function test_can_find_users_by_their_username()
{
$createdUser = factory(User::class)->create(['username' => 'janedoe']);
$foundUser = User::findByUsername('johndoe');
}
}
phpunit.xml
<php>
<env name="APP_ENV" value="testing" />
<env name="CACHE_DRIVER" value="testing" />
<env name="SESSION_DRIVER" value="testing" />
<env name="QUEUE_DRIVER" value="testing" />
<env name="DB_CONNECTION" value="testing" />
<env name="" value=":memory:" />
</php>
<?php
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
todo: See how to run only one test