Comments & Discussion
For some reason the below test is throwing an error and i cant seem to figure out why.
public function test_can_upload_image() { Storage::fake('activities');
$company = Company::factory()->create(); $user = User::factory()->companyOwner()->create(['company_id' => $company->id]); $guide = User::factory()->guide()->create(); $file = UploadedFile::fake()->image('avatar.jpg'); $this->actingAs($user)->post(route('companies.activities.store', $company), [ 'name' => 'activity', 'description' => 'description', 'start_time' => '2023-09-01 10:00', 'price' => 9999, 'guide_id' => $guide->id, 'image' => $file, ]); Storage::disk('activities')->assertExists($file->hashName()); Storage::disk('activities')->assertExists('thumbs/'.$file->hashName());}
I get the follwing error
FAILED Tests\Feature\CompanyActivityTest > can upload image
Unable to find a file or directory at path [o6Kjk8xnngz9MFO2D5E3DwJcneAEp3xpPXy23HPA.jpg].
Failed asserting that false is true.
at vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:119 115▕ 116▕ $paths = Arr::wrap($path); 117▕ 118▕ foreach ($paths as $path) { ➜ 119▕ PHPUnit::assertTrue( 120▕ $this->exists($path), "Unable to find a file or directory at path [{$path}]." 121▕ ); 122▕ 123▕ if (! is_null($content)) {
+1 vendor frames
2 tests/Feature/CompanyActivityTest.php:86
With Storage::fake('activities') we would fake a so-called disk named 'activities'. In our filesystems.php there's no disk declared as 'activities'.
In our CompanyActivityController we're saving to the subdirectory 'activities' on 'public' disk, so for making our test work, we should fake this disk:
Storage::fake('public');
Also use this disk then for the assertions:
Storage::disk('public')->assertExists('/activities/'.$file->hashName());
Hi all,
Awesome tutorial, I'm learning a bunch of new stuff, however I can get pass testing phase in this step. From 14 tests ony 4 are passing, and I see 2 recurring issues:
Call to undefined relationship [activities] on model [App\Models\Company].
and
FAILED Tests\Feature\CompanyActivityTest > admin can edit activity for company ────────────────────────────────────────────────
Error Error
Class "Database\Factories\Factory" not foundat database\factories\ActivityFactory.php:9 5▕ use App\Models\Company; 6▕ use Illuminate\Support\Carbon; 7▕ 8▕ ➜ 9▕ class ActivityFactory extends Factory 10▕ { 11▕ public function definition(): array 12▕ { 13▕ return [
1 database\factories\ActivityFactory.php:9 2 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:829
Have I missed some steps (I have been doing this for the last few hours, and it is a posibility, although, I went with copy/past last time to ensure no typing error sneaks in) ?
The second issue us due to not having an import declared on ActivityFactory.php because, again is not mentioned anywhere in here that is should be.
use Illuminate\Database\Eloquent\Factories\Factory;
I'm down to my last issue, and this one I have no clue on how to solve it:
FAILED Tests\Feature\CompanyActivityTest > guides are shown only for specific company in edit form AssertionFailedError
The response is not a view.at tests\Feature\CompanyActivityTest.php:147 143▕ $guide2 = User::factory()->guide()->create(['company_id' => $company2->id]); 144▕ 145▕ $response = $this->actingAs($user)->get(route('companies.activities.edit', [$company, $activity])); 146▕ ➜ 147▕ $response->assertViewHas('guides', function (Collection $guides) use ($guide) { 148▕ return $guide->name === $guides[$guide->id]; 149▕ }); 150▕ 151▕ $response->assertViewHas('guides', function (Collection $guides) use ($guide2) {
Tests: 1 failed, 13 passed (29 assertions) Duration: 3.92s
Any suggestion on how I can overcome this obstacle?
Much appreciated!
Having a bit of an issue with your use statement and the activity::class it seems to have a mismatch,
Yes, correct, well spotted! Fixed now.
Sorry but I just realized that you never created the Activity Model or at least I can’t find it.
I can see where you added
All the database and models have been created in the first lesson.
The existing price values are divided by 100 in front-end as you mention after adding accesor/mutator. Creating new activities, the price value is multiplied by 100 and saved in the DB, but in front-end the value is the same as the one entered. what is the goal for the new values??