Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resolve error within phpunit concerning assertRedirectTo() following zend framework 2.1 example, caused by trailing slash?
    primarykey
    data
    text
    <p>After following the Zend Framework 2 <a href="http://framework.zend.com/manual/2.1/en/user-guide/overview.html" rel="nofollow">"Getting Started"</a> tutorial successfully, I started on the <a href="http://framework.zend.com/manual/2.1/en/tutorials/unittesting.html" rel="nofollow">Unit testing example for version 2.1</a> and, following it verbatim, I get a failure with PHPUnit:</p> <pre><code>Failed asserting response redirects to "/album", actual redirection is "/album/" </code></pre> <p>Here's the relevant bit of code from the <code>IndexControllerTest.php</code> file:</p> <pre><code>public function testAddActionRedirectsAfterValidPost() { $albumTableMock = $this-&gt;getMockBuilder('Album\Models\AlbumTable') -&gt;disableOriginalConstructor() -&gt;getMock(); $albumTableMock-&gt;expects($this-&gt;once()) -&gt;method('saveAlbum') -&gt;will($this-&gt;returnValue(null)); $serviceManager = $this-&gt;getApplicationServiceLocator(); $serviceManager-&gt;setAllowOverride(true); $serviceManager-&gt;setService('Album\Models\AlbumTable', $albumTableMock); $postData = array('title' =&gt; 'Led Zeppelin III', 'artist' =&gt; 'Led Zeppelin'); $this-&gt;dispatch('/album/add', 'POST', $postData); $this-&gt;assertResponseStatusCode(302); $this-&gt;assertRedirectTo('/album'); } </code></pre> <p>Here's what I have within the <code>addAction()</code> function within <code>AlbumController.php</code>:</p> <pre><code> // Redirect to list of albums return $this-&gt;redirect()-&gt;toRoute('album'); </code></pre> <p>The <code>.htaccess</code> file is the one that came with the Skeleton Application and to be thorough, I've tested that in the browser, <code>http://localhost/zf2-tut/album</code> and <code>http://localhost/zf2-tut/album/</code> both resolve properly and that in the process of adding an entry, it does indeed redirect me back to the url with the trailing slash.</p> <p>I know this might come off as just a bit persnickety but I'd like to learn the proper way to resolve an issue such as this. </p> <p>I know I can change the assertion test to include the trailing slash but since the example doesn't follow that and the toRoute() method does not have the trailing slash, I assume that maybe I've made a mistake in my code or environment somewhere and I assume it's bad practice to have a test deviate from what it's testing.</p> <p>What's the best way to handle this? If I made a change somewhere that causes the url to resolve with the trailing slash, I actually prefer that in this case but want my code to be clean and not leave any ambiguity about it so I'd want any bits about the route to clearly indicate the trailing slash is expected.</p> <p>FYI, I made a small change in my folder structure and namespaces after following the tutorial, in changing "Model" to "Models" as a matter of personal preference. That in no way impacts this issue (afaik) but I wanted to point that out so it's not a red herring.</p> <hr> <p>Edit: Shortly after I posted my question, with a bit more legwork I realized how the trailing slash is being appended to my route in this case and posted that in an answer. However, per my answer, I would like to understand why Zend Framework 2 is appending the slash when it's supposedly optional and also, how to properly test for it with PHPUnit.</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