Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MarkOmeje's avatar

TEST ERROR

I'm getting this error in my test

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

C:\xampp\htdocs\laravel\birdboard\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:148
C:\xampp\htdocs\laravel\birdboard\tests\Feature\ManageProjectTest.php:17

FAILURES!
Tests: 9, Assertions: 15, Failures: 1, Warnings: 1.

How can i solve it. And again i've been following Laracasts TDD series but i can't still create projects for weeks now. It doesnt show any error: it just reloads the page. My controller

<?php

namespace App\Http\Controllers;

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

class ProjectsController extends Controller
{
    public function index() {
        $projects = auth()->user()->projects;
        return view('projects.index', compact('projects'));
    }

    public function store() {
        $attributes = request()->validate(['title' => 'required', 'description' => 'required']);
        $project = auth()->user()->projects()->create($attributes);
        return redirect('/projects');
    }

    public function create() {
        return view('projects.create');
    }

    public function show(Project $project) {
        if (auth()->id() !== (int)$project->owner_id) {
            abort(403);
        }
        return view('projects.show', compact('project'));
    }

}

My model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    protected $guarded = [];

    public function path() {
        return "/projects/{$this->id}";
    }

    public function owner() {
        return $this->belongsTo(User::class);
    }

}

My Test script

<?php

namespace Tests\Feature;

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

class ManageProjectTest extends TestCase {
    use WithFaker, RefreshDatabase;

    /** @test */
    public function guests_cannot_manage_projects() {
        $project = factory('App\Project')->raw();
        $this->signIn();
        $this->get('/projects/create')->assertRedirect('login');
        $this->get($project->path())->assertRedirect('login');
        $this->post('/projects', $project->toArray())->assertRedirect('login');
    }

    /** @test */
    public function a_user_can_create_a_project() {
        $this->actingAs(factory('App\User')->create());
        $this->get('projects/create')->assertStatus(200);
        $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']);
    }

    /** @test */
    public function a_user_can_view_their_project() {
        $this->be(factory('App\User')->create());
        $project = factory('App\Project')->create(['owner_id' => auth()->id()]);
        $this->get($project->path())->assertSee($project->title)->assertSee($project->description);
    }
    
    /** @test */
    public function an_authenticated_user_cannot_view_the_project_of_others() {
        $this->be(factory('App\User')->create());
        $project = factory('App\Project')->create();
        $this->get($project->path())->assertStatus(403);
    }

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

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


}

Thanks.

0 likes
3 replies
tykus's avatar
public function guests_cannot_manage_projects()
{
        $project = factory('App\Project')->raw();
        $this->signIn();
    //

You sign in, so you're no longer a guest. Don't sign in.

MarkOmeje's avatar

Thank You. Please still help on this error which was the next after implementing your solution.

Error: Call to a member function path() on array

C:\xampp\htdocs\laravel\birdboard\tests\Feature\ManageProjectTest.php:19
bugsysha's avatar
$project = factory('App\Project')->raw();

Returns an array and does not create a record in the database. So you can not call method path() which is defined on Project model since it is an array.

Please or to participate in this conversation.