testing - Authentication test running strange -
I just tried to write a simple test for Auth
:
as fun as m; ... public function testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome () {$ auth = m :: fake ( "illuminate \ Authentication \ AuthManager '); $ Auth-> ShouldReceive ('Guest') - & gt; Once () - & gt; AndReturn (right); $ This- & gt; Call ('GET', '/'); $ This- & gt; AssertRedirectedToRoute ('general.welcome'); } Public function testHomeWhenUserIsAuthenticatedThenRedirectToDashboard () {$ auth = m :: fake ('Roshan / Auth / AuthManager'); $ Auth-> ShouldReceive ('Guest') - & gt; Once () - & gt; AndReturn (wrong); $ This- & gt; Call ('GET', '/'); $ This- & gt; AssertRedirectedToRoute ('dashboard.overview'); }
This code is:
public function getHome () {if (Auth :: guest ()) {back direct :: route ('general .welcome '); } Return redirects: route ('dashboard overview'); }
When I run, I get the following error:
EF ..... time: 265 ms, memory: 13.00 mb 1 the error was: 1) PagesControllerTest :: testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome joke \ exceptions \ InvalidCountException: method include () must be called one time but actually Mockery_0_Illuminate_Auth_AuthManager called 0 times. - 1 Failure was: 1) PagesController Test :: TestMovageUserIs are officially redirectedTabashboard failed to emphasize two strings are identical. --- Expected +++ Real @@ @@ - 'http: // localhost / dashboard / overview' + 'http: // localhost / welcome'
My questions are: < / P>
-
Why is the error output different on two similar test cases?
-
In the case of the second test, why not a fake
Auth :: guest ()
is called, is it unsuccessful? -
Is there a way to write better test for my code? Or to test even better code.
-
At the top of the test cases, I use
fun
,AuthManager
but if I mask to ridiculeAuth :: shoudReceive () - & gt; Once () -> & Return ()
to use, then it does eventually workfun
andAuth :: fake
is no separation between the mask here ?
Thank you.
A new instance of the illuminate \ Authentication \ AuthManager
and no access Auth
mask that is being used by your function getHome ()
ergo, your fake example will never be found. (Standard disclaimer that have not been tested in any code of the following.)
Try it:
public function testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome () {Auth :: shouldceceive ( ' Guest ') - & gt; -; AndReturn (right), once ()> gt $ This-> Call ('GET', '/'); $ This- & gt; AssertRedirectedToRoute ('general.welcome'); } Public Function testHomeWhenUserIsAuthenticatedThenRedirectToDashboard () {Authentication :: shouldReceive ('Guest') - & gt; Once () - & gt; AndReturn (wrong); $ This- & gt; Call ('GET', '/'); $ This- & gt; AssertRedirectedToRoute ('dashboard.overview'); }
If you check brighten up \ support \ facades \ facade
, then you will see that it takes care of the joke for you if you actually do it like this You wanted to do this (by making an example of a fake example of Ethics), you have to inject it in some way under the code according to the test. I believe that something like this can be done, assuming that you extend from the Testise classes provided by Laurel:
public function testHomeWhenUserIsNotAuthenticatedThenRedirectToWelcome () {$ this- & Gt; App ['auth'] = $ Auth = m: fake ('illuminating / AuthManager'); // line up your mask with 'auth' mask will swap out $ Auth-> ShouldReceive ('Guest') - & gt; Once () - & gt; AndReturn (right); $ This- & gt; Call ('GET', '/'); $ This- & gt; AssertRedirectedToRoute ('general.welcome'); }
Comments
Post a Comment