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) ✗
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']));
}
}
Related
I am trying to make a login from laravel 8 but at the begging I faced an error which I cannot find a solution. The UsersTablesSeeder is created but still the compiler cannot find it
Illuminate\Contracts\Container\BindingResolutionException
Target class [UsersTablesSeeder] does not exist.
at C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
828▕
829▕ try {
830▕ $reflector = new ReflectionClass($concrete);
831▕ } catch (ReflectionException $e) {
➜ 832▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
833▕ }
834▕
835▕ // If the type is not instantiable, the developer is attempting to resolve
836▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionException::("Class "UsersTablesSeeder" does not exist")
2 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionClass::__construct("UsersTablesSeeder")
the following code shows DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call(UsersTablesSeeder::class);
}
}
this is my user table
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
class UsersTablesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
User::create([
'name' => 'John Smith',
'email' => 'john_smith#gmail.com',
'password' => Hash::make('password'),
'remember_token' => str_random(10),
]);
}
}
I am following this link
Add namespace Database\Seeders; to your class. As said in laravel 8
Seeders and factories are now namespaced. To accommodate for these
changes, add the Database\Seeders namespace to your seeder classes. In
addition, the previous database/seeds directory should be renamed to
database/seeders:
I'm getting error as on below screenshot
Please help me i am trying and for this i have spend too much time but still issue no luck.
I am using laravel official dusk package for front-web testing. When i am running login test case its showing error "users_email_unique" which is above showing in picture. I am using use DatabaseTransactions but it is not reverting my last transaction. For this i am using test database also. Here is my my code:
<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LoginTest extends DuskTestCase
{
use DatabaseTransactions;
/**
* A Dusk test example.
*
* #return void
*/
public function test_I_can_login_successfully()
{
$user = factory(User::class)->create([
'email' => 'login#gmail.com',
'password' => bcrypt('password'),
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/')
->type('email', 'login#gmail.com')
->type('password', 'password')
->press('Login')
->assertSee($user->name);
});
}
}
For Dusk you cannot use use DatabaseTransactions. You need to use DatabaseMigrations instead. When you update trait then you should get rid of the error.
You have sample Dusk test in documentation and it looks like this:
<?php
namespace Tests\Browser;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Chrome;
use Tests\DuskTestCase;
class ExampleTest extends DuskTestCase
{
use DatabaseMigrations;
/**
* A basic browser test example.
*
* #return void
*/
public function testBasicExample()
{
$user = factory(User::class)->create([
'email' => 'taylor#laravel.com',
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->type('email', $user->email)
->type('password', 'password')
->press('Login')
->assertPathIs('/home');
});
}
}
As you see it's also using DatabaseMigrations trait.
I have an error when I run my test in laravel:
Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)
Here is the test code:
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Book;
class BookReservationTest extends TestCase
{
use RefreshDatabase;
/** #test */
public function a_book_can_be_added_to_the_library()
{
$this->withoutExceptionHandling();
$response = $this->post('/books', [
'title' => 'cool book title',
'author' => 'victor'
]);
$response->assertOk();
$this->assertCount(11, Book::all());
}
}
It's giving me this error when I run the test:
undefined variable $faker.
This is the WithFaker file.
https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Testing/WithFaker.php
<?php
namespace Tests\Unit;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class LoginTest extends TestCase
{
use WithFaker;
/**
* A basic test example.
*
* #return void
*/
/** #test */
public function test_example()
{
$user = User::create([
'username' => $faker->firstName(),
]);
}
}
You have to use $this->faker->firstName() not just $faker->firstName()
Update 1
Now when we use WithFaker Trait $this->faker will give us null, to get around this make sure to call $this->setupFaker() first.
e.g.
class SomeFactory
{
use WithFaker;
public function __construct()
{
$this->setUpFaker();
}
}
credit #Ebi
For anyone coming here from 2021. We no longer require the addition of
$this->setUpFaker();
You only need to include the trait as described in the accepted answer.
once you completed installation of Faker.
include autoload file and create instance
$faker = \Faker\Factory::create();
$faker->firstname()
$faker->lastname()
For more information visit
check you seed function run (Faker $faker).
I have tried so much to get this database seed to work but I still get Class 'Account' not found even though I have namespaced where I should.
There error is thrown when running php artisan db:seed on $accountOut = Account::create(array( where Account is what is throwing the error. Am stating the using incorrectly? If I were to remove all the namespacing I have no issues at all.
My Account.php file:
<?php namespace App\Models;
class Account extends \Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'account';
/**public function user()
{
return $this-belongsTo('User');
}*/
}
My seed file:
<?php
use App\Models;
class TransactionSeeder extends Seeder {
public function run()
{
DB::table('transaction')->delete();
DB::table('account')->delete();
$accountOut = Account::create(array(
'name' => 'Checking',
'origin' => 'Bank'
));
$accountIn = Account::create(array(
'name' => 'Stuff',
'origin' => 'Expense'
));
$adminUser = Sentry::getUserProvider()->findByLogin('admin#admin.com');
Transaction::create(array(
'account_id_in' => $accountIn->id,
'account_id_out' => $accountOut->id,
'amount' => 300.00
));
}
}
I feel really stupid but instead of calling out use App\Models you would call out use App\Models\Account and it works as it should.
Then remember to run php composer.phar dump-autoload
I've had similar issues and prefixing my classes with the namespace operator solved them.
Try \Account