Wednesday, November 23, 2011

Re: Write unit test for a controller that uses AuthComponent in CakePHP 2.0.3

The only thing I can suggest is to add the mocked method to your
generate function :

$this->Users = $this->generate('Users', array( 'components'
=> array('Session', 'Auth' => array('user') ) ));

Not sure if it would mock the method otherwise.


On Nov 23, 6:13 am, elitalon <elita...@gmail.com> wrote:
> I am using the ControllerTestCase, so I have modified the generated
> controller using the following code, which is a slightly modified
> version of your suggested approach:
>     $this->Users = $this->generate('Users', array(      'components'
> => array('Session', 'Auth')    ));    $this->Users->Auth->expects($this->once())      ->method('user')      ->with('id')      -
> >will($this->returnValue(1));
>
> But it still fails. I completely agree with AuthComponent being
> inconsisten between the website usage and testing. I have flagged a
> ticket and I'm in discussion with Mark Story about that.
> On Nov 22, 7:04 pm, Shukuboy <shuku...@gmail.com> wrote:
>
> > Hi,
>
> > I've also had to deal with testing controllers that use Auth
> > lately.    Auth has improved heaps since 1.3 but it still seems to be
> > coupled with various bits and pieces of the core, and hence you might
> > get different behaviours between the website and testing.
>
> > I recommend mocking out Auth and getting it to return the value you
> > expect, in this case if your logged in user is 1, you can use
> > something like this :
> > $Users->Auth->expects($this->once())->method('user')
> >        -> with( 'id' )
> >         ->will($this->returnValue(1));
>
> > Check out
> > -http://book.cakephp.org/2.0/en/development/testing.html#using-mocks-w...
> > and if you need more info on phpunit :
> > -http://www.phpunit.de/manual/current/en/test-doubles.html#test-double...
>
> > The new move to phpunit in Cake 2, was a great idea.  It's quite
> > powerful and allows you to do almost everything you need while unit
> > testing.
>
> > Hope this helps,
> > Shuku

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate