Am trying to adapt TDD on laravel, but am getting trouble to test controller which queries a collection of records and returns view with data when I run my test it returns an error message that trying to get the id of undefined property
// my test
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
class JobApplicationTest extends TestCase
{
use WithoutMiddleware;
/** @test */
public function user_can_see_all_open_positions()
{
// when user visit apply for a job page
$this->withoutExceptionHandling();
$response = $this->get(route('positions.open'));
$response->assertSuccessful();
$response->assertViewIs('positions.open.index');
$response->assertViewHas('positions');
}
}
// My controller
public function open(Request $request)
{
//TODO support additional filters & searches
//initialize query
$query = Position::query()->which()->are()->open();
//paginate query result
$positions = $query->paginate(config('app.defaults.pageSize'));
$data = [
'route_title' => 'Open Job Positions',
'route_description' => 'Available Job Positions',
'positions' => $positions,
];
return view('positions.open.index', $data);
}
so the problem is when I run text it looks view is returned with no data while view references data returned to the controller
error message when running test
1) Tests\Unit\JobApplicationTest::user_can_see_all_open_positions ErrorException: Trying to get property 'id' of non-object (View: /var/www/html/ipf-projects/niajiri/resources/views/pos itions/open/index.blade.php)
<h3>
<a href="{{ route('positions.preview', [
'id' => $position->id,
'slug' => str_slug($position->title)
]) }}" class="text-navy">
{!! $position->title !!} - {!! $position->organization->name !!}
</a>
</h3>