that it's not a mocked model.
So what am I exactly mocking using `$this->generate()`?
On Nov 30, 1:41 pm, elitalon <elita...@gmail.com> wrote:
> I have posted this question in StackOverflow and Jose Lorenzo answered
> me this:
>
> "The actual correct answer should be using mock objects instead of
> actually login the user in manually:
>
> $this->controller = $this->generate('Users', array(
> 'components' => array('Auth' => array('user')) //We mock the
> Auth Component here
> ));
> $this->controller->Auth->expects($this->once())->method('user') //
> The method user()
> ->with('id') //Will be called with first param 'id'
> ->will($this->returnValue(2)) //And will return something for
> me
> $this->testAction('/users/edit/2', array('method' => 'get'));"
>
> He is actually right but I'm still having problem when dealings with
> models. For example, suppose I am testing an action that uses `save`
> and `create` methods of a model. I tried to configure the expectations
> like this:
>
> $this->Users->User->expects($this->once())->with('save')->will($this->returnValue(true));
>
> $this->testAction('/signup', array('data' => $data));
>
> The action actually calls `save` to create a new User, but PHPUnit
> says that it was never called. I have created `$this->Users`
> controller in `ControllerTestCase::setUp()` like this:
>
> $this->Users = $this->generate('Users', array(
> 'components' => array('Session', 'Auth' => array('user'))
> 'models' => 'User'
> ));
>
> According to documentation, not passing any arguments to a mocked
> models implies every method is stubbed. So what am I doing wrong here?
>
> Thanks!
>
> On Nov 23, 1:25 pm, Shukuboy <shuku...@gmail.com> wrote:
>
>
>
>
>
>
>
> > 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
No comments:
Post a Comment