Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class evidenceController {
public function getDetails($id){
$course = Course::with(['courseType'])
->checkEstablishmentOnView()
->excludeArchived()
->findOrFail($id);
$view = View::make('courses/parts/details')
->with('course', $course);
return Response::json(['html' => $view->render()]);
}
}
@taylorotwell

This comment has been minimized.

Copy link

@taylorotwell taylorotwell commented Dec 2, 2016

You need to split this into two tests. Inject a repository into the controller with a single method that performs this query which you can easily Mock, then test the repository itself with an actual database call.

class EvidenceController
{
    public function __construct(CourseRepository $courses)
    {
          $this->courses = $courses;
    }

    public function getDetails($id)
    {
        $course = $this->courses->getDetails($id);
        $view = View::make('etc');
    }
}
// In Test...

$courses = Mockery::mock('CourseRepository');
$courses->shouldReceive('getDetails')->with('1')->andReturn($something);
$this->app->instance(CourseRepository::class, $courses);

$this->post('/evidence/details/1');
@yogasukma

This comment has been minimized.

Copy link

@yogasukma yogasukma commented Dec 2, 2016

hi @MarkBaker i interested in your code, i knew it just sample, but what is ->checkEstablishmentOnView()->excludeArchived() ? i usually use something like that just for accessor and mutator. can i see sample of that model to learn what you did with it?

@sailingdeveloper

This comment has been minimized.

Copy link

@sailingdeveloper sailingdeveloper commented Dec 2, 2016

@ysupr he is probably using Eloquent Scopes. Look it up in the documentation, it's quite handy :)

@ramirezd42

This comment has been minimized.

Copy link

@ramirezd42 ramirezd42 commented Dec 2, 2016

@taylorotwell you are only showing an example the first test you describe correct? Or am i misunderstanding. How do you go about testing the acual database call?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 2, 2016

@ramirezd42 With integration tests instead of unit tests.

@martinbean

This comment has been minimized.

Copy link

@martinbean martinbean commented Dec 6, 2016

Returning a HTML blob in a JSON response?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.