2

I try to test my controller actions in laravel with mockery. I already read this tutorial here:

http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/

I use DI in my constructor like this:

public function __construct(User $user, Cartalyst\Sentry\Sentry $sentry)
{
    $this->user = $user;
    $this->sentry = $sentry;

    ...

}

My problem is the following code in my Controller:

public function getShow($id)
{
    try
    {
        // this is a problem, because I dont know how to tell mockery, to mock the
        // Userprovider
        $user = $this->sentry->getUserProvider()->findById($id);
        // this is not a problem for me
        $this->user->all();

        ...

I am trying to work with Mockery as a mock framework. My question is how to mock a call like $this->sentry->getUserProvider() (Cartalyst Sentry is a advanced authorization bundle). To mock the User Model i write:

$this->user = Mockery::mock('Eloquent', 'User');

Any idea how to mock the Userprovider or should I handle this in another way ? I want to test my UserController if I am getting the user details depending on the id.

1
  • 1
    You mock the sentry and have it return a mocked userProvider. $sentryMock->shouldReceive('getUserProvider')->times(1)->andReturn($userProviderMock); Commented Nov 20, 2013 at 13:06

1 Answer 1

3

You can stub the getUserProvider method to return another stub, e.g.

$sentry = m::mock("F\Q\C\N\Sentry");
$userProvider = m::mock("F\Q\C\N\UserProvider");
$sentry->shouldReceive("getUserProvider")->andReturn($userProvider)->byDefault();

$userProvider->shouldReceive("findById")->andReturn(new User);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.