Good morning friends
this time I have a easy one for you but I am not experienced enough to track the bug ...
I get the following error:
Error : Call to a member function connection() on null
<?php
namespace Tests\Unit;
use App\Models\Offer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\TestCase;
class OfferTest extends TestCase
{
use RefreshDatabase;
// protected $offer;
// public function setUp(): void
// {
// parent::setUp(); // TODO: Change the autogenerated stub
// $this->offer = Offer::factory()->create();
// }
/** @test */
function an_offer_has_a_creator()
{
$offer = Offer::factory()->create();
$offer->assertInstanceOf(User::class, $this->offer->user);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOffersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('offers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('Name');
$table->decimal('latitude', 8, 6);
$table->decimal('longitude', 9, 6);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('offers');
}
}
<?php
namespace Database\Factories;
use App\Models\Offer;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class OfferFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Offer::class;
public function definition ()
{
return [
'Name' => 'Breath Work',
'latitude' => 51.567952 ,
'longitude' => 8.080428 ,
];
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Offer extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
}
also a side question ... ist the without exception handling not done anymore?