SoftwarEng92's avatar

Laravel PHPUnit test giving error

I'm follow this series https://laracasts.com/series/build-a-laravel-app-with-tdd in the 3. video I've an issue. I'm write what jeffrey wrote but error still coming up in front of me.

This is ProjectsTest Class

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ProjectsTest extends TestCase
{
    use WithFaker, RefreshDatabase;

    /** @test */
    public function a_user_can_create_a_project()
    {
        $this->withoutExceptionHandling();

        $attributes = [
            'title' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

        $this->post('/projects', $attributes)->assertRedirect('/projects');
        $this->assertDatabaseHas('projects', $attributes);
        $this->get('/projets')->assertSee($attributes['title']);

    }

    /** @test */
    public function a_project_requires_a_title()
    {
        $attributes = factory('App\Project')->raw(['title' => '']);
        $this->post('/projects', $attributes)->assertSessionHasErrors('title');
    }

    /** @test */
    public function a_project_requires_a_description()
    {
        $attributes = factory('App\Project')->raw(['description' => '']);
        $this->post('/projects', $attributes)->assertSessionHasErrors('description');
    }
}

This part of code from ProjectsController

namespace App\Http\Controllers;

use App\Project;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    public function index()
    {
        $projects = Project::all();
        return view('projects.index', compact('projects'));
    }

    public function store()
    {
        //validate
        $attributes =  request()->validate(['title' => 'required', 'description' => 'required']);
        //persist
        Project::create($attributes);
        //redirect
        redirect('/projects');
    }
}

And that code from ProjectFactory.php

use Faker\Generator as Faker;

$factory->define(App\Project::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'description' => $ƒaker->paragraph
    ];
});

Finally error is,

PHPUnit 7.5.7 by Sebastian Bergmann and contributors.

FEE 3 / 3 (100%)

Time: 174 ms, Memory: 22.00 MB

There were 2 errors:

  1. Tests\Feature\ProjectsTest::a_project_requires_a_title ErrorException: Undefined variable: ƒaker

/Users/talhaisler/birdboard/database/factories/ProjectFactory.php:8 /Users/talhaisler/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:273 /Users/talhaisler/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:246 /Users/talhaisler/birdboard/tests/Feature/ProjectsTest.php:33

  1. Tests\Feature\ProjectsTest::a_project_requires_a_description ErrorException: Undefined variable: ƒaker

/Users/talhaisler/birdboard/database/factories/ProjectFactory.php:8 /Users/talhaisler/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:273 /Users/talhaisler/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:246 /Users/talhaisler/birdboard/tests/Feature/ProjectsTest.php:40

--

There was 1 failure:

  1. Tests\Feature\ProjectsTest::a_user_can_create_a_project Response status code [200] is not a redirect status code. Failed asserting that false is true.

/Users/talhaisler/birdboard/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:148 /Users/talhaisler/birdboard/tests/Feature/ProjectsTest.php:24

ERRORS! Tests: 3, Assertions: 1, Errors: 2, Failures: 1.

UPDATED: I found the error, I wrote like that is not normal "f" :)) $ƒaker->paragraph

0 likes
1 reply
diegoaurino's avatar
Level 39

Hello, @softwareng92!

I found a typo on:

'description' => $ƒaker->paragraph

It should be "f" and not the unicode "ƒ".

Let me know if it helps.

1 like

Please or to participate in this conversation.