dimanche 25 avril 2021

Controller returning null on eloquent query in laravel 5.8 phpunit test

I am trying to create a mock for model ProjectRating where I want to return a specific value based on whatever the query for the model is. With select and first I am receiving the set result value but I am getting null when I have a where in the query.

My Query in the controller looks like this

$projectRating = ProjectRating::select('rating')
                ->where('project_id', $projectId)
                ->where('country_id', $countryId)
                ->where('language_id', $languageId)
                ->first() ;

and my test code looks like this

$fakeProjectRating = \Mockery::mock(ProjectRating::class);

$fakeProjectRating->shouldReceive('select->where->where->where->first')->andReturn(['rating'=>'c']);

app()->instance(ProjectRating::class, $fakeProjectRating);

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire