-1

This is the unit test which I want to run. This is always failing. I have tried with wrong data. Still it fails and with correct data still it fails. Please someone help me out from this.

If anyone wants to explore more: http://github.com/PawanRoy1997/forum.git

Php Unit Test:

<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    /** @@test */
    public function login()
    {
        $data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
        User::create($data);
        $this->assertTrue(Auth::attempt($data));
    }
}

Output:

➜  forum git:(master) ✗ asn test

   FAIL  Tests\Unit\UserTest
  ⨯ 

   FAIL  Tests\Feature\UserTest
  ⨯ login

  ---

  • Tests\Unit\UserTest > 
   Error 

  Call to a member function connection() on null

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1571
    1567▕      * @return \Illuminate\Database\Connection
    1568▕      */
    1569▕     public static function resolveConnection($connection = null)
    1570▕     {
  ➜ 1571▕         return static::$resolver->connection($connection);
    1572▕     }
    1573▕ 
    1574▕     /**
    1575▕      * Get the connection resolver instance.

      +7 vendor frames 
  8   tests/Unit/UserTest.php:19
      Illuminate\Database\Eloquent\Model::__callStatic()

  • Tests\Feature\UserTest > login
  Failed asserting that false is true.

  at tests/Feature/UserTest.php:19
     15▕     public function login()
     16▕     {
     17▕         $data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
     18▕         User::create($data);
  ➜  19▕         $this->assertTrue(Auth::attempt($data));
     20▕     }
     21▕ }
     22▕ 


  Tests:  2 failed
  Time:   0.31s

➜  forum git:(master) ✗ 
Pawan Roy
  • 29
  • 5
  • 1
    You no need to test auth - it's tested by Laravel. – Maksim Jul 20 '21 at 11:22
  • I am not able login bro. then i found Auth::attempt is not working. Then I wrote this test. – Pawan Roy Jul 21 '21 at 05:20
  • @PawanRoy the error is pretty clear... `Call to a member function connection() on null`... `connection()` is `null`, for sure you have not set `.env` or `config\database.php` correctly, that is the error... – matiaslauriti Jul 21 '21 at 06:38
  • Actually, I just updated the laravel. It is not hashing my password while creating the user model. So I decided to manually Hash it. And it worked as expected. – Pawan Roy Jul 21 '21 at 13:07
  • @PawanRoy: Please consider to accept your answer (this should be possible after some time), this will mark the question as answered and will help future visitors. – hakre Jul 21 '21 at 18:47

1 Answers1

1

Actually, I just figured it out that I need to hash the password while creating the user. I forgot to mention that I have updated Laravel then all of this started. In laravel 8.51, Auth::attempt checks for hashed passwords only. But in laravel 8.50 and before, it allows password to be not hashed.

<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class UserTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $data = [
            'name' => 'someone',
            'email' => 'some@some.com',
            'password' => Hash::make('password'),
            'confirm_password' => 'password'
        ];
        User::create($data);

        $this->assertDatabaseCount('users', 1);

        $this->assertTrue(Auth::attempt(['email' => 'some@some.com', 'password' => 'password']));
    }
}
Pawan Roy
  • 29
  • 5