Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel Controller UNIT Testing Issue?
    primarykey
    data
    text
    <p>I am creating a Laravel 4 app and i have run into a little snag. While writing tests for my controller i noticed for some strange reason it can never seem to validate. Heres my (stripped down) controller codes.</p> <pre><code>&lt;?php use Controllers\Base\PublicController; class GuestController extends PublicController { /** * Display the report issue form. * * @return null */ public function getReportIssue() { $this-&gt;layout-&gt;title = Lang::get('app.report_issue'); $this-&gt;layout-&gt;content = View::make('guest.report_issue'); } public function postReportIssue() { $rules = [ 'full_name' =&gt; 'required|min:2|max:100', 'email' =&gt; 'required|email', 'issue' =&gt; 'required|min:10|max:1000', ]; $validator = Validator::make(Input::all(), $rules); if ($validator-&gt;fails()) { return Redirect::route('guest.report_issue') -&gt;withInput() -&gt;withErrors($validator-&gt;messages()); } return Redirect::route('guest.reported_issue') -&gt;with('msg', 'Okay'); } } </code></pre> <p>Now he tests ive created for the two methods above are...</p> <pre><code>public function testHandleFailReportIssue() { Input::replace([ 'full_name' =&gt; '', 'email' =&gt; '', 'issue' =&gt; '', ]); $this-&gt;call('POST', 'report-issue'); $this-&gt;assertRedirectedToRoute('guest.report_issue'); $this-&gt;assertSessionHasErrors(['full_name', 'email', 'issue']); } public function testHandlePassReportIssue() { Input::replace([ 'full_name' =&gt; 'John Doe', 'email' =&gt; 'john@localhost.dev', 'issue' =&gt; 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar. Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar' ]); $this-&gt;call('POST', 'report-issue'); $this-&gt;assertRedirectedToRoute('guest.reported_issue', [], ['msg']); } </code></pre> <p>The initial test passes successfully, but the second one fails. After a little investigation, it shows that the Validation was not passing, which means that the <code>Input::replace()</code> method is not doing its job, because i injected valid request values. Maybe im missing something please?</p> <p>[EDIT]</p> <p>I decided to do this</p> <pre><code>public function testHandlePassReportIssue() { Input::replace([ 'full_name' =&gt; 'John Doe', 'email' =&gt; 'john@flashdp.com', 'issue' =&gt; 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar. Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar', ]); $response = $this-&gt;route('POST', 'guest.report_issue'); dd($this-&gt;app['session']-&gt;get('errors')); $this-&gt;assertRedirectedToRoute('guest.reported_issue', [], ['msg']); } </code></pre> <p>on the test to debug by inspecting the session and like i suspected, the Input was not getting populated, any reason this could have happened? The validation messages were returned.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload