Any help would be appreciated.
Jan 10, 2016
13
Level 54
Trying Out Some Refactoring For My Login Test
I figure I'm pretty much done with my login testing and wanting to see if anyone felt like I was missing something or should redo how I"m assigning properties from the setUp function. Please look carefully as I might have made some mistakes.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LoginTest extends TestCase
{
use DatabaseTransactions;
public function setUp()
{
parent::setUp();
$this->signIn(['email' => 'john@example.com', 'password' => bcrypt($this->password)]);
}
/** @test */
public function a_user_can_successfully_log_in()
{
$this->visit(route('login'));
$this->type($this->user->email, 'email');
$this->type($this->password, 'password');
$this->press('Login');
$this->seePageIs(route('dashboard'));
}
/** @test */
public function a_user_receives_errors_for_wrong_login_credentials()
{
$this->visit(route('login'));
$this->type($this->user->email, 'email');
$this->type('testpass456', 'password');
$this->press('Login');
$this->see('These credentials do not match our records.');
}
/** @test */
public function a_user_is_redirected_to_dashboard_if_logged_in_and_tries_to_access_login_page()
{
$this->visit(route('login'));
$this->seePageIs(route('dashboard'));
}
}
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
protected $user;
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
public function signIn($user = null)
{
if (! $user) {
$user = factory(App\User::class)->create();
}
$this->user = $user;
$this->actingAs($this->user);
return $this;
}
}
Level 11
@xtremer360 ie, your test could be changed to this. --
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LoginTest extends TestCase
{
use DatabaseTransactions;
public function setUp()
{
parent::setUp();
$plainPassword = 'test';
$this->user = factory(App\User::class)->create([
'email' => 'john@example.com',
'password' => bcrypt($plainPassword),
]);
$this->user->plainPassword = $plainPassword;
}
/** @test */
public function a_user_can_successfully_log_in()
{
$this->visit(route('login'));
$this->type($this->user->email, 'email');
$this->type($this->user->plainPassword, 'password');
$this->press('Login');
$this->seePageIs(route('dashboard'));
}
/** @test */
public function a_user_receives_errors_for_wrong_login_credentials()
{
$this->visit(route('login'));
$this->type($this->user->email, 'email');
$this->type('invalid-password', 'password');
$this->press('Login');
$this->see('These credentials do not match our records.');
}
/** @test */
public function a_user_is_redirected_to_dashboard_if_logged_in_and_tries_to_access_login_page()
{
$this->actingAs($this->user);
$this->visit(route('login'));
$this->seePageIs(route('dashboard'));
}
}
and if needed, move out lines 15-20 of the setup to the parent class and call it setupUser
1 like
Please or to participate in this conversation.