Skip to main content
Richard A. Hoyle avatar

This is a weird one at least for me 7 passed and 1 failed that one was the first one. Any suggestions?

 
php artisan test --filter=CompanyGuideTest
 
FAIL Tests\Feature\CompanyGuideTest
company owner can view his companies guides 0.30s
company owner cannot view other companies guides 0.03s
company owner can create guide to his company 0.05s
company owner cannot create guide to other company 0.03s
company owner can edit guide for his company 0.03s
company owner cannot edit guide for other company 0.03s
company owner can delete guide for his company 0.03s
company owner cannot delete guide for other company 0.03s
──────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\Feature\CompanyGuideTest > company owner can view his companies guides
Expected: \n
\n
\n
\n
\n
\n
\n
 
To contain: Mr. Albin Hackett PhD
 
at tests\Feature\CompanyGuideTest.php:24
20
21 $response = $this->actingAs($user)->get(route('companies.guides.index', $company->id));
22
23 $response->assertOk()
24 ->assertSeeText($secondUser->name);
25 }
26
27 public function test_company_owner_cannot_view_other_companies_guides()
28 {
 
 
Tests: 1 failed, 7 passed (15 assertions)
Duration: 0.68s

PS the /n goes on for quite a wile like 20 to 30 lines.

Richard A. Hoyle avatar

could the problem be the fact that I don't have a second user? but would't that be supplied by the UserFactory or the CompanyFactory ?

Ali Al Qahtani avatar

In CompanyGuideController index method

should query the guide role id like this:

$guides = $company->users()->where('role_id', RoleEnum::GUIDE->value)->get();

jwinder avatar

Either its me, or shouldnt the last two tests be written like this?

public function test_company_owner_can_delete_guides_from_their_company() { $company = Company::factory()->create();

$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.destroy', [$company->id,$guide->id]));
 
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertSoftDeleted('users', [
'name' => $guide->name,
'email' => $guide->email
]);
}
 
 
public function test_company_owner_cannot_delete_guides_from_another_company()
{
$company = Company::factory()->create();
$companyTwo = Company::factory()->create();
 
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $companyTwo->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.destroy', [$companyTwo->id,$guide->id]));
 
$response->assertForbidden();
}
Ali Al Qahtani avatar

In resources/views/companies/guides/edit.blade.php

Undefined variable $user should be $guide

Nerijus avatar

Updated lesson. Thanks.

Luis Antonio Parrado avatar
Luis Antonio Parrado

in resources/views/companies/guides/edit.blade.php: line 12 the action route for the form must be companies.guides.update instead of companies.users.update.

Povilas Korop avatar

Thank you, well noticed, fixed it!

joschuba avatar

In the tests of this section: Wouldn't it be more explicit to check, if the company owner can edit a guide and not himself?

Before:

public function test_company_owner_can_edit_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->put(route('companies.guides.update', [$company->id, $user->id]), [
'name' => 'updated user',
'email' => 'test@update.com',
]);
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertDatabaseHas('users', [
'name' => 'updated user',
'email' => 'test@update.com',
'company_id' => $company->id,
]);
}

After:

public function test_company_owner_can_edit_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->put(route('companies.guides.update', [$company->id, $guide->id]), [
'name' => 'updated user',
'email' => 'test@update.com',
]);
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertDatabaseHas('users', [
'name' => 'updated user',
'email' => 'test@update.com',
'company_id' => $company->id,
]);
}

The same for the other. Plus and an additional point: It should be tested, if the actual data of the user/guide is removed, that was created with the factory. With assertDatabaseMissing() the test would would always pass, because the given data was never in the database. And because auf the introduction of soft deletes a few chapters back, it should also be replaced by assertSoftDeleted() .

Before:

public function test_company_owner_can_delete_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.update', [$company->id, $user->id]));
 
$response->assertRedirect(route('companies.guides.index', $company
david avatar

Hi, is there a different in using:

$response = $this->actingAs($user)->get(route('companies.guides.index', $company->id));

or

$response = $this->actingAs($user)->get(route('companies.guides.index', $company));

in both cases the test passes without errors, how laravel interprets them? thanks

Nerijus avatar

It is how route model binding wourks and has nothing to do with tests itself. You can read more about it in the laravel

👍 1
teebee avatar

StoreGuideRequest is missing use statement for Illuminate\Validation\Rules. This should be explicit for the users that do not use LSP.