dimanche 31 juillet 2016

Laravel 5.1 - Why unit testing authentication fails with a model factory?

So using the factory method the 1st and 3rd test fail.

<?php

class AuthTest extends TestCase
{
     public function setUp()
     {
         parent::setUp();

         $this->artisan('migrate');
         //$this->artisan('db:seed');
         $user = factory(RMS\User::class, 'admin')->create([
             'email' => 'admin@abc.co.za',
             'password' => bcrypt('secreT5000'),
          ]);

          $normal_user = factory(RMS\User::class)->create([
              'email' => 'company@abc.co.za',
              'password' => bcrypt('secreT5000'),
           ]);
     }

     public function tearDown()
     {
         $this->artisan('migrate:reset');
     }

    /**
     * Test Successful Login
     *
     * @return void
     */
    public function testGoodAdminLogin()
    {
      $this->visit('/admin')
         ->type('admin@abc.co.za', 'email')
         ->type('secreT5000', 'password')
         ->check('remember')
         ->press('Login')
         ->seePageId('/dashboard')
         ->dontSee('These credentials do not match our records.')
         ->see('Users');
    }

    /**
     * Test Failed Login
     *
     * @return void
     */
    public function testFailedLogin()
    {
      $this->visit('/admin')
         ->type('admin@abc.co.za', 'email')
         ->type('secreT', 'password')
         ->press('Login')
         ->see('These credentials do not match our records.')
         ->seePageIs('/auth/login');
    }

    /**
     * Test Normal user positive Login
     *
     * @return void
     */
    public function testNormalUserLogin()
    {
      $this->visit('/admin')
         ->type('company@abc.co.za', 'email')
         ->type('secreT5000', 'password')
         ->press('Login')
         ->seePageIs('dashboard')
         ->dontSee('Users');
    }
}

However using the model save method the tests pass:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class AuthTest extends TestCase
{
    //Run Migrations to Create Tables
    use DatabaseMigrations;

    public function setUp(){
      parent::setUp();
    }

    /**
     * Test Successful Login
     *
     * @return void
     */
    public function testGoodAdminLogin()
    {
      //Add the Test Admin User
      DB::table('users')->insert([
          'email' => 'admin@abc.co.za',
          'password' => bcrypt('secreT5000'),
          'is_admin' => true
      ]);

      $this->visit('/admin')
         ->type('admin@abc.co.za', 'email')
         ->type('secreT5000', 'password')
         ->check('remember')
         ->press('Login')
         ->seePageIs('/dashboard')
         ->see('Users');
    }

    /**
     * Test Failed Login
     *
     * @return void
     */
    public function testFailedLogin()
    {
      //Add the Test Admin User
      DB::table('users')->insert([
          'email' => 'admin@abc.co.za',
          'password' => bcrypt('secreT5000'),
          'is_admin' => true
      ]);

      $this->visit('/admin')
         ->type('admin@abc.co.za', 'email')
         ->type('secreT', 'password')
         ->press('Login')
         ->see('These credentials do not match our records.')
         ->seePageIs('/auth/login');
    }

    /**
     * Test Failed Login
     *
     * @return void
     */
    public function testNormalUserLogin()
    {
      //Add the Test User
      DB::table('users')->insert([
          'email' => 'company@abc.co.za',
          'password' => bcrypt('secreT5000'),
          'is_admin' => false
      ]);

      $this->visit('/admin')
         ->type('company@abc.co.za', 'email')
         ->type('secreT5000', 'password')
         ->press('Login')
         ->seePageIs('dashboard')
         ->dontSee('Users');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire