2008年11月26日星期三

Re: [fw-mvc] Zend_Test_PHPUnit - assertRedirectTo failing

-- afx114 <jsnodspam@gmail.com> wrote
(on Wednesday, 26 November 2008, 05:14 PM -0800):
> Matthew Weier O'Phinney-3 wrote:
> >
> > Use this as the assertion -- it'll give you more information:
> >
> > $this->assertRedirectTo('/auth/login',
> > var_export($this->response->getHeaders()));
> >
> > That should show you what headers were actually set in the response
> > object, and may give more insight into why the test is failing.
> >
>
> Thanks, this was definitely helpful, and here is what it is giving me:
>
> array (
> 0 =>
> array (
> 'name' => 'Location',
> 'value' => 'http://www.domain.com/auth/login',
> 'replace' => true,
> ),
> )
>
> If I switch my assertRedirectTo to this, it passes:
>
> $this->assertRedirectTo('http://www.domain.com/auth/login');
>
> I believe the domain is getting prepended to the redirect Location value
> because I am setting baseUrl in a view helper:
>
> $frontController->setBaseUrl($config->baseUrl);
>
> If I set $config->baseUrl to empty, then assertRedirectTo('/auth/login')
> passes as expected.

Aha -- that makes sense. In that case, you might want to use
assertRedirectRegex():

$this->assertRedirectRegex('#/auth/login$#');

which would match the *end* of the URL -- the segment you're actually
checking. Alternately, pull the baseUrl from the request object when
testing using assertRedirectTo():

$this->assertRedirectTo($this->request->getBaseUrl() . '/auth/login');

which would also test that the base URL is getting set correctly.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: