Laravel mockery

I am trying to set up the simplest tests in my controller, but like most of Laravel's other things, there are no decent tutorials to demonstrate simple things.

I can run a simple test (in the UserControllerTest file), for example:

public function testIndex()
{      
    $this->call('GET', 'users');
    $this->assertViewHas('users');
}

This calls the / users route and passes it to the users of the array.

I want to do the same with Mockery, but how?

If I try this:

public function testIndex()
{
  $this->mock->shouldReceive('users')->once();

  $this->call('GET', 'users');

}

I get the error "Static method Mockery_0_users :: all does not exist in this breadboard object.

Why not? I am mocking a User who expands Rage and, in turn, expands Eloquence. Why :: everything does not exist for the layout?

By the way, these are the settings for Mockery:

public function setUp()
{
  parent::setUp();

  $this->mock = $this->mock('User');
}

public function mock($class)
{
  $mock = Mockery::mock($class);

  $this->app->instance($class, $mock);

  return $mock;
}
+4
2

Eloquent. Eloquent - . Laravel , .

User, . - , . Laravel Google.

, , :

class UserController extends BaseController {

    public function __construct(UserRepositoryInterface $users)
    {
        $this->users = $users;
    }

    public function index()
    {
        $users = $this->users->all();
        return View::make('user.index', compact('users'));
    }

}

class UserControllerTest extends TestCase
{

    public function testIndex()
    {
        $repository = m::mock('UserRepositoryInterface');
        $repository->shouldReceive('all')->andReturn(new Collection(array(new User, new User)));
        App::instance('UserRepositoryInterface', $repository);

        $this->call('GET', 'users');
    }

}

, , ​​ ... .

+4

apiato.io, Laravel, , - IoC, , :

/**
 * Mocking helper
 *
 * @param $class
 *
 * @return  \Mockery\MockInterface
 */
public function mock($class)
{
    $mock = Mockery::mock($class);
    App::instance($class, $mock);

    return $mock;
}
0

All Articles