Level 102
What?
Summer Sale! All accounts are 50% off this week.
How can I get the user name to pass the tests ???
auth()->user()->name
What?
I have an observer...
public function created(Subscription $subscription)
{
Log::channel('subscription')
->info('Subscription '. $subscription->id .' was created by '. auth()->user()->name);
}
If I run the tests, I get an error about the user name
Attempt to read property "name" on null
@MimisK What test? So far you have shown 0 tests
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create([
'name' => 'TestUser',
'email' => '[email protected]',
'password' => bcrypt('password')
]);
factory(Role::class)->create(['id' => 1, 'title' => 'Admin']);
}
/** @test */
public function a_user_can_store_subscription()
{
factory(Permission::class)->create(['id' => 1, 'title' => 'subscription_create']);
Role::findOrFail(1)->permissions()->sync(1, 1);
User::findOrFail(1)->roles()->sync(1);
$member = factory(Member::class)->create([
'city_id' => factory(City::class)->create(),
'insurance_fund_id' => factory(InsuranceFund::class)->create(),
'status_id' => factory(Status::class)->create(),
]);
$response = $this->actingAs($this->user)->get(route('admin.subscriptions.create', ['member' => $member->id]));
$response->assertSee('Create');
$response->assertSee($member->full_name);
$response = $this->post(route('admin.subscriptions.store', ['member' => $member->id]), [
'member_id' => $member->id,
'year' => '01-01-2022',
'amount' => 30.00,
'notes' => 'Subscription 2022'
]);
$this->assertDatabaseHas('subscriptions', [
'member_id' => $member->id
]);
$response->assertRedirect();
}
@MimisK
Missing $this->actingAs($this->user)->
$response = $this->actingAs($this->user)->post(route('admin.subscriptions.store', ['member' => $member->id]), [
'member_id' => $member->id,
'year' => '01-01-2022',
'amount' => 30.00,
'notes' => 'Subscription 2022'
]);
I finally solved it with this addition to the setUp
\Auth::login($this->user);
$this->assertAuthenticatedAs($this->user);
Please or to participate in this conversation.