Reputation: 105
I've got the following factory in Laravel 5.7, when invoking it nothing is being returned:
<?php
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Model;
$factory->define(App\Record::class, function (Faker $faker) {
return [
"name" => $faker->name,
];
});
Whereas my model is:
<?php
namespace App;
use App\Product;
use Illuminate\Database\Eloquent\Model;
class Record extends Model
{
protected $table = "records";
protected $fillable = ["name"];
function __construct()
{
parent::__construct();
}
}
And I'm invoking the factory here:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App;
use App\Product;
use App\Record;
use App\User;
class RecordTest extends TestCase
{
use RefreshDatabase;
use WithoutMiddleware;
/** @test */
public function when_record_page_for_existing_record_is_accessed_then_a_product_is_displayed()
{
//$record = factory(App\Record::class)->make();
$record = factory(App\Record::class)->create();
echo "\n\n$record->name\n\n";
}
}
when printing
$record->name
I'm getting nothing, not null, no empty string, just nothing. What seems to be the problem? If I save whatever is generated by the factory into a variable instead of returning it right away I can see that name is being populated, but after returning it nothing happens, it's gone
Upvotes: 3
Views: 3003
Reputation: 219920
This piece of code is the problematic part:
function __construct()
{
parent::__construct();
}
You're not passing the attributes to the parent constructor. Eloquent accepts the model's attributes in the constructor, but your overriding constructor doesn't accept them, nor pass them up to the parent.
Change it to this:
function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
BTW, you're overriding Eloquent's constructor, but you're not doing anything in there. Are you sure you actually want to override it?
Upvotes: 15
Reputation: 1005
By default phpunit
won't print your echo
.
To print it, please use phpunit -v
Upvotes: 1