Summer Sale! All accounts are 50% off this week.

learningMonkey's avatar

validation code

public function store(Request $request)
    {
        //validate
        request()->Validate([
            'title' => 'required',
            'description'=> 'required'
        ]);

        //creating data (storing)
        Project::create(request(['title', 'description']));
        
        //redirect
        return redirect('/projects');
        
    }
0 likes
8 replies
learningMonkey's avatar

@Tray2 showing me this error

 Session is missing expected key [errors].
Failed asserting that false is true.
learningMonkey's avatar

@SilenceBringer test code

namespace Tests\Feature;

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

class ProjectsTest extends TestCase
{
    use WithFaker, RefreshDatabase;
    public function test_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('/projects')->assertSee($attributes['title']);
   
    }

    public function test_a_project_requires_a_title() 
    {
        $this->post('/projects', [])->assertSessionHasErrors('title');
    }

    
    public function test_a_project_requires_a_description() 
    {
        $this->post('/projects', [])->assertSessionHasErrors('description');
    }
}

SilenceBringer's avatar
Level 56

@jaspal I'm little confused with the uppercased V

request()->Validate

try to replace it to lowercase validate

1 like

Please or to participate in this conversation.