php - How to mock Auth in a Laravel 5.2 Modl -


i'm trying write unit tests brand new mini app. write functional tests me branching out try , mocking , stubs , things make code.

the model looks :

<?php  namespace app;  use illuminate\database\eloquent\model; use auth;  class mymodel extends model {     public static function getuser()     {         $user = \auth::user();         return $user->adldapuser->cn[0];     } } 

and test :

class mymodeltest extends testcase {      public function testgetuser()     {         $mockresult = new stdclass();         $mockresult->adldapuser = new stdclass();         $mockresult->adldapuser->cn=array("test");         $auth = $this->getmock('auth');         $auth             ->method('user')             ->will($this->returnvalue($mockresult));          $this->assertequals('test',\app\mymodel::getuser());     } } 

but when run unit test following error message :

there 1 error:

1) mymodeltest::testgetuser errorexception: trying property of non-object

/home/aidan/web/vagrant-web-dev/src/apps/orcid/app/mymodel.php:61 /home/aidan/web/vagrant-web-dev/src/apps/orcid/tests/mymodeltest.php:18

and if post out $user it's null.

what doing wrong here?

here go again answering own question half hour after asking it.

for prosperity's sake i'll leave here.

the answer in post here : https://stackoverflow.com/a/17602763/808124

so replaced $this->getmock with

auth::shouldreceive('user')->once()->andreturn($mockresult); 

working


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -