testing - Authentication test running strange -
i've tried write simple test auth
:
use mockery m; ... public function testhomewhenuserisnotauthenticatedthenredirecttowelcome() { $auth = m::mock('illuminate\auth\authmanager'); $auth->shouldreceive('guest')->once()->andreturn(true); $this->call('get', '/'); $this->assertredirectedtoroute('general.welcome'); } public function testhomewhenuserisauthenticatedthenredirecttodashboard() { $auth = m::mock('illuminate\auth\authmanager'); $auth->shouldreceive('guest')->once()->andreturn(false); $this->call('get', '/'); $this->assertredirectedtoroute('dashboard.overview'); }
this code:
public function gethome() { if(auth::guest()) { homecoming redirect::route('general.welcome'); } homecoming redirect::route('dashboard.overview'); }
when run, i've got next error:
ef..... time: 265 ms, memory: 13.00mb there 1 error: 1) pagescontrollertest::testhomewhenuserisnotauthenticatedthenredirecttowelcome mockery\exception\invalidcountexception: method guest() mockery_0_illuminate_auth_authmanager should called 1 times called 0 times. — there 1 failure: 1) pagescontrollertest::testhomewhenuserisauthenticatedthenredirecttodashboard failed asserting 2 strings equal. --- expected +++ actual @@ @@ -'http://localhost/dashboard/overview' +'http://localhost/welcome'
my questions are:
two similar test cases why error output differs? first 1 mock auth::guest()
not called while sec 1 seems called.
on sec test case, why fail?
is there way write improve tests code above? or improve code test.
above test cases, utilize mockery
mock authmanager
, if utilize facade auth::shoudreceive()->once()->andreturn()
, works eventually. there different between mockery
, auth::mock
facade here?
thanks.
you're mocking new instance of illuminate\auth\authmanager
, not accessing auth
facade beingness utilized function gethome()
. ergo, mock instance never called. (standard disclaimer none of next code tested.)
try this:
public function testhomewhenuserisnotauthenticatedthenredirecttowelcome() { auth::shouldreceive('guest')->once()->andreturn(true); $this->call('get', '/'); $this->assertredirectedtoroute('general.welcome'); } public function testhomewhenuserisauthenticatedthenredirecttodashboard() { auth::shouldreceive('guest')->once()->andreturn(false); $this->call('get', '/'); $this->assertredirectedtoroute('dashboard.overview'); }
if check out illuminate\support\facades\facade
, you'll see takes care of mocking you. if wanted way doing (creating instance of mock instance of auth), you'd have somehow inject code under test. believe done assuming extend testcase class provided laravel:
public function testhomewhenuserisnotauthenticatedthenredirecttowelcome() { $this->app['auth'] = $auth = m::mock('illuminate\auth\authmanager'); // above line swap out 'auth' facade facade. $auth->shouldreceive('guest')->once()->andreturn(true); $this->call('get', '/'); $this->assertredirectedtoroute('general.welcome'); }
testing laravel phpunit mockery laravel-testing
No comments:
Post a Comment