Framework: Mocking the user and auth models

Created on 6 Apr 2013  路  1Comment  路  Source: laravel/framework

I had my first go at this problem a couple of days ago, but I just spent a good chunk of my day trying to figure this out with the help of people in the irc. However I simply have not found a solution.

All I would like to do is to test the following code:

$user = Auth::user();
$posts = $user->posts();

To do this as I see it I would have to mock User and Auth. For that reason I would need to implement dependency injection in my users model. Okay that can be done easy task:

User.php
protected $posts;
protected $auth;

public function __construct(
    \Repositories\PostRepositoryInterface $post
    \Repositories\AuthRepositoryInterface $auth
) {
    $this->user = $posts;
    $this->auth    = $auth;
}

However now when I run $this->auth->attempt($credentials) the user object that is returned will not have the needed dependencies causing my code to fail.

Error and stacktrace

1ErrorException: Catchable Fatal Error: Argument 1 passed to Models\User::__construct() must be an instance of Repositories\PostsRepositoryInterface, none given, called in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php on line 89 and defined in /Users/Andreas/Sites/reteam/mind16/app/models/User.php line 64
in /Users/Andreas/Sites/reteam/mind16/app/models/User.php line 64
at ErrorHandler->handle('4096', 'Argument 1 passed to Models\User::__construct() must be an instance of Repositories\PostsRepositoryInterface, none given, called in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php on line 89 and defined', '/Users/Andreas/Sites/reteam/mind16/app/models/User.php', '64', array()) in /Users/Andreas/Sites/reteam/mind16/app/models/User.php line 64
at User->__construct() in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php line 89
at EloquentUserProvider->createModel() in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php line 56
at EloquentUserProvider->retrieveByCredentials(array('email' => '[email protected]', 'password' => 'admin')) in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Auth/Guard.php line 188
at Guard->attempt(array('email' => '[email protected]', 'password' => 'admin'))
at call_user_func_array(array(object(Guard), 'attempt'), array(array('email' => '[email protected]', 'password' => 'admin'))) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 10889
at Manager->__call('attempt', array(array('email' => '[email protected]', 'password' => 'admin'))) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 4325
at AuthManager->attempt(array('email' => '[email protected]', 'password' => 'admin')) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 4325
at Facade::__callStatic('attempt', array(array('email' => '[email protected]', 'password' => 'admin'))) in /Users/Andreas/Sites/reteam/mind16/app/models/User.php line 204
at Auth::attempt(array('email' => '[email protected]', 'password' => 'admin')) in /Users/Andreas/Sites/reteam/mind16/app/models/User.php line 204
at User::login('[email protected]', 'admin') in /Users/Andreas/Sites/reteam/mind16/app/repositories/EloquentUserRepository.php line 24
at EloquentUserRepository::login('[email protected]', 'admin') in /Users/Andreas/Sites/reteam/mind16/app/controllers/SessionsController.php line 35
at SessionsController->store()
at call_user_func_array(array(object(SessionsController), 'store'), array()) in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php line 138
at Controller->callMethod('store', array()) in /Users/Andreas/Sites/reteam/mind16/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php line 115
at Controller->callAction(object(Application), object(Router), 'store', array()) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 7089
at Router->Illuminate\Routing\{closure}()
at call_user_func_array(object(Closure), array()) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 13369
at Route->callCallable() in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 13348
at Route->run(object(Request)) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 7109
at Router->dispatch(object(Request)) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 958
at Application->dispatch(object(Request)) in /Users/Andreas/Sites/reteam/mind16/bootstrap/compiled.php line 946
at Application->run() in /Users/Andreas/Sites/reteam/mind16/public/index.php line 51

Most helpful comment

Just mock the facade:

Auth::shouldReceive('user')->andReturn($user = m::mock('StdClass'));

$user->shouldReceive('posts')->once()->andReturn(array('posts'));

>All comments

Just mock the facade:

Auth::shouldReceive('user')->andReturn($user = m::mock('StdClass'));

$user->shouldReceive('posts')->once()->andReturn(array('posts'));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

felixsanz picture felixsanz  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

digirew picture digirew  路  3Comments