Re: testing a controller's add action

2009-12-31 Thread mark_story
I don't have an issue if someone wants to copy/paste/modify the
article I wrote into the book. There is no need to file tickets about
improving the documentation on testing, you can simply edit/add to the
existing documentation instead.  testAction as you are aware is
exceptionally limited in what it is capable of doing, and regular
testing methods are clearer and easier to work with.

I also use mocks in my controller tests, and covered using mocks in a
different post too. 
http://www.mark-story.com/posts/view/testing-cakephp-controllers-mock-objects-edition.
I find I usually mock Auth, Session and Email components as they are
the most disruptive to automated testing.

As for planned improvements to the test suite, I didn't really have
much planned, there are some fixes and improvements to be done with
the code coverage, but as far as making controller testing easier, I
didn't have anything planned.

-Mark

On Dec 29, 4:04 am, Lorenzo Bettini  wrote:
> Thanks for all the information!
>
> Let's see how testing is in the next release, and in particular the
> documentation; as for proposing an alternative in the book I guess I
> should file a ticket, right?
>
> For the moment I'm testing controllers' actions the "hard way" :)
>
> cheers
>         Lorenzo
>
>
>
>
>
> nurvzy wrote:
> > Lorenzo Bettini  wrote:
> >> I'm not getting errors: as I said before, I can see that data is not
> >> populated using the debugger, and also because it executes the
> >> instructions to be executed when data is null; thus it basically does
> >> not faulter, it simply does not get any data...  can it be a bug?
>
> > It's possible, I never had a whole lot of luck with testAction other
> > than just really basic tests, as such I almost never use it.  It's
> > possible we're missing something really simple.  /shrug.  testAction
> > does a lot of the 'magic' on it own, but I prefer doing things in such
> > a way I have complete control over the test -- less surprises that
> > way.  I read, in 1.3/2.0 the testsuite is getting a major makeover.
>
> >> yes, with the instructions in this post I can successfully test 
> >> mycontroller'sadd and edit actions.
>
> >> But I guess the documentation of cakephp should be updated to include
> >> such way oftestingcontrollers, especially since testAction, as you
> >> also noticed, is not powerful enough (and probably buggy due to the
> >> missing data ;)
>
> > I agree, I like the way Mark tests his controllers, it makes a lot
> > more sense to me.   If you're feeling generous go ahead and post an
> > alternative in the Book, they always like people helping them make the
> > documentation better.
>
> --
> Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
> HOME:http://www.lorenzobettini.itMUSIC:http://www.purplesucker.com
> BLOGS:http://tronprog.blogspot.com http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: testing a controller's add action

2009-12-29 Thread Lorenzo Bettini
Thanks for all the information!

Let's see how testing is in the next release, and in particular the 
documentation; as for proposing an alternative in the book I guess I 
should file a ticket, right?

For the moment I'm testing controllers' actions the "hard way" :)

cheers
Lorenzo

nurvzy wrote:
> Lorenzo Bettini  wrote:
>> I'm not getting errors: as I said before, I can see that data is not
>> populated using the debugger, and also because it executes the
>> instructions to be executed when data is null; thus it basically does
>> not faulter, it simply does not get any data...  can it be a bug?
> 
> It's possible, I never had a whole lot of luck with testAction other
> than just really basic tests, as such I almost never use it.  It's
> possible we're missing something really simple.  /shrug.  testAction
> does a lot of the 'magic' on it own, but I prefer doing things in such
> a way I have complete control over the test -- less surprises that
> way.  I read, in 1.3/2.0 the testsuite is getting a major makeover.
> 
>> yes, with the instructions in this post I can successfully test 
>> mycontroller'sadd and edit actions.
>>
>> But I guess the documentation of cakephp should be updated to include
>> such way oftestingcontrollers, especially since testAction, as you
>> also noticed, is not powerful enough (and probably buggy due to the
>> missing data ;)
> 
> I agree, I like the way Mark tests his controllers, it makes a lot
> more sense to me.   If you're feeling generous go ahead and post an
> alternative in the Book, they always like people helping them make the
> documentation better.
> 



-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: testing a controller's add action

2009-12-28 Thread nurvzy
Lorenzo Bettini  wrote:
> I'm not getting errors: as I said before, I can see that data is not
> populated using the debugger, and also because it executes the
> instructions to be executed when data is null; thus it basically does
> not faulter, it simply does not get any data...  can it be a bug?

It's possible, I never had a whole lot of luck with testAction other
than just really basic tests, as such I almost never use it.  It's
possible we're missing something really simple.  /shrug.  testAction
does a lot of the 'magic' on it own, but I prefer doing things in such
a way I have complete control over the test -- less surprises that
way.  I read, in 1.3/2.0 the testsuite is getting a major makeover.

> yes, with the instructions in this post I can successfully test 
> mycontroller'sadd and edit actions.
>
> But I guess the documentation of cakephp should be updated to include
> such way oftestingcontrollers, especially since testAction, as you
> also noticed, is not powerful enough (and probably buggy due to the
> missing data ;)

I agree, I like the way Mark tests his controllers, it makes a lot
more sense to me.   If you're feeling generous go ahead and post an
alternative in the Book, they always like people helping them make the
documentation better.

> what do you mean by Mocking the Auth Component?

Well, instead of using the actual Auth Component, you'd use a Mocked
stub of that component with "dummy" functions you can set to return
whatever you want.  SimpleTest comes with a great Mocking Class that
does all the work for you.  A simple Mock would look something like
this:

App::import('Component', 'Auth');
App::import('Controller', 'SomeController');

function startTest(){
  $this->SomeController = new TestSomeController();
  $this->SomeController->Components->initialize($this-
>SomeController);
  Mock::generate('AuthComponent');
  $this->SomeController->Auth = new MockAuthComponent();
}

That little setup will overwrite the Auth of your Controller with the
Mocked Auth generated by Mock::generate('string_of_class_to_mock').
The mock creates a dummy class that has all the same methods as the
actual class but everything returns null by default.  So what's the
point?  Well, you can change what the MockAuthComponent's functions
return on the fly to better suite your tests.

Here's an example, lets say in your controller you're doing something
like:

//In SomeController action...
$stuff = $this->SomeModel->findByUserId($this->Auth->user('id'));
$this->set('stuff', $stuff);

In you're test you can decide what $this->Auth->user('id') returns
without touching the Auth.User.id in the session.

//Back in the test...
function testSomeAction(){
  $this->SomeController->Auth->setReturnValue('user',1); //$this->Auth-
>user(...) will return 1.
  $this->SomeController->someAction();
  $this->assertTrue(!empty($this->SomeController->viewVars
['stuff'])); //Assert that the stuff variable has something in it.
}

As you can see, we never had to write Auth.User.id to the session to
get the desired result.  This is a much more focused test than what
Mark suggests in the link I gave you in my previous post.  This test
really hones in on the action, not the auth component as well.

You can do a whole lot with mocks, like only mock certain functions
and letting the other ones be.  Here is the documentation:
http://www.simpletest.org/en/mock_objects_documentation.html


Hope that helps,
Happy testing,
Nick
http://www.webtechnick.com


> > On Dec 27, 2:28 am, Lorenzo Bettini  wrote:
> >> Yes, it uses a redirect (inside the method), but the problem comes up
> >> before reaching a possible redirect: empty($this->data) is true when
> >> entering the method itself...
>
> >> As for the redirect I think I already handle that with
>
> >> class TestPapers extends PapersController {
> >>         var $autoRender = false;
>
> >>      function redirect($url, $status = null, $exit = true) {
> >>          $this->redirectUrl = $url;
> >>      }
>
> >> but the problem, as I said, it's that no data is passed to theaction...
>
> >> John Andersen wrote:
> >>> Is your addactionusing a redirect?
> >>>    John
> >>> On Dec 26, 6:48 pm, Lorenzo Bettini  wrote:
>  Hi
>  I already managed to test some actions of controllers using testAction
>  method; these were actions that were expected to return something.
>  Now, I'd like to test an addactionof thecontroller, thus, following
>  the book I did:
>  $data = array(
>          'Paper' => array(
>                  'title' => 'MyTitle',
>                  'year' => 2009
>          ));
>  debug($data);
>  $results = $this->testAction(
>          array(
>          'controller' => 'papers',
>          'action' => 'admin_add',
>          ),
>          array('data' => $data, 'method' => 'post')
>          );
>  but when executing thecontroller'saction(I tried that with the
>  debug), the $this->data is always empty and uninit

Re: testing a controller's add action

2009-12-28 Thread Lorenzo Bettini
nurvzy wrote:
> testAction will not use your TestPapers class that I assume you've
> created in your test.  It will faulter at the redirect.  I'm not sure
> why $this->data is not being populated by testAction in your admin_add
> action as you seem to be doing it correctly..  How do you know its not
> being populated?  What errors are you getting?
> 

I'm not getting errors: as I said before, I can see that data is not 
populated using the debugger, and also because it executes the 
instructions to be executed when data is null; thus it basically does 
not faulter, it simply does not get any data...  can it be a bug?

> while testAction has a lot going for it, its limitations prevent me
> from using it, among other things test that use testAction wont run in
> the CLI testsuite cake shell.  So I avoid using it and just test my
> controllers the "hard way" As pointed out by Mark Story:
> 
> http://www.mark-story.com/posts/view/testing-cakephp-controllers-the-hard-way
> 
> In short, you have the right thing going with your TestPapers, simply
> instantiate it into a Papers var it within your TestCase and call
> functions on it directly.  This way you can just set $this->Papers-
>> data directly and then call $this->Papers->admin_add();  Since you've
> overwritten redirect in your TestPapers class you'll be able to assert
> the correct redirects.
> 

yes, with the instructions in this post I can successfully test my 
controller's add and edit actions.

But I guess the documentation of cakephp should be updated to include 
such way of testing controllers, especially since testAction, as you 
also noticed, is not powerful enough (and probably buggy due to the 
missing data ;)

> 
> Follow Mark's guide (link above), as he walks you through testing the
> admin_edit of a posts controller.  You'll almost certainly be able to
> copy/paste most of it.  Although I suggest Mocking the Auth Component
> instead of writing Auth.user to the Session.   Just my opinion.
> 

what do you mean by Mocking the Auth Component?

thanks
Lorenzo

> Hope that helps,
> Nick
> http://www.webtechnick.com
> 
> On Dec 27, 2:28 am, Lorenzo Bettini  wrote:
>> Yes, it uses a redirect (inside the method), but the problem comes up
>> before reaching a possible redirect: empty($this->data) is true when
>> entering the method itself...
>>
>> As for the redirect I think I already handle that with
>>
>> class TestPapers extends PapersController {
>> var $autoRender = false;
>>
>>  function redirect($url, $status = null, $exit = true) {
>>  $this->redirectUrl = $url;
>>  }
>>
>> but the problem, as I said, it's that no data is passed to the action...
>>
>>
>>
>> John Andersen wrote:
>>> Is your add action using a redirect?
>>>John
>>> On Dec 26, 6:48 pm, Lorenzo Bettini  wrote:
 Hi
 I already managed to test some actions of controllers using testAction
 method; these were actions that were expected to return something.
 Now, I'd like to test an add action of the controller, thus, following
 the book I did:
 $data = array(
 'Paper' => array(
 'title' => 'MyTitle',
 'year' => 2009
 ));
 debug($data);
 $results = $this->testAction(
 array(
 'controller' => 'papers',
 'action' => 'admin_add',
 ),
 array('data' => $data, 'method' => 'post')
 );
 but when executing the controller's action (I tried that with the
 debug), the $this->data is always empty and uninitialized...  where am I
 going wrong?
 thanks in advance
 Lorenzo
>> --



-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: testing a controller's add action

2009-12-27 Thread nurvzy
testAction will not use your TestPapers class that I assume you've
created in your test.  It will faulter at the redirect.  I'm not sure
why $this->data is not being populated by testAction in your admin_add
action as you seem to be doing it correctly..  How do you know its not
being populated?  What errors are you getting?

while testAction has a lot going for it, its limitations prevent me
from using it, among other things test that use testAction wont run in
the CLI testsuite cake shell.  So I avoid using it and just test my
controllers the "hard way" As pointed out by Mark Story:

http://www.mark-story.com/posts/view/testing-cakephp-controllers-the-hard-way

In short, you have the right thing going with your TestPapers, simply
instantiate it into a Papers var it within your TestCase and call
functions on it directly.  This way you can just set $this->Papers-
>data directly and then call $this->Papers->admin_add();  Since you've
overwritten redirect in your TestPapers class you'll be able to assert
the correct redirects.


Follow Mark's guide (link above), as he walks you through testing the
admin_edit of a posts controller.  You'll almost certainly be able to
copy/paste most of it.  Although I suggest Mocking the Auth Component
instead of writing Auth.user to the Session.   Just my opinion.

Hope that helps,
Nick
http://www.webtechnick.com

On Dec 27, 2:28 am, Lorenzo Bettini  wrote:
> Yes, it uses a redirect (inside the method), but the problem comes up
> before reaching a possible redirect: empty($this->data) is true when
> entering the method itself...
>
> As for the redirect I think I already handle that with
>
> class TestPapers extends PapersController {
>         var $autoRender = false;
>
>      function redirect($url, $status = null, $exit = true) {
>          $this->redirectUrl = $url;
>      }
>
> but the problem, as I said, it's that no data is passed to the action...
>
>
>
> John Andersen wrote:
> > Is your add action using a redirect?
> >    John
>
> > On Dec 26, 6:48 pm, Lorenzo Bettini  wrote:
> >> Hi
>
> >> I already managed to test some actions of controllers using testAction
> >> method; these were actions that were expected to return something.
>
> >> Now, I'd like to test an add action of the controller, thus, following
> >> the book I did:
>
> >> $data = array(
> >>         'Paper' => array(
> >>                 'title' => 'MyTitle',
> >>                 'year' => 2009
> >>         ));
> >> debug($data);
> >> $results = $this->testAction(
> >>         array(
> >>         'controller' => 'papers',
> >>         'action' => 'admin_add',
> >>         ),
> >>         array('data' => $data, 'method' => 'post')
> >>         );
>
> >> but when executing the controller's action (I tried that with the
> >> debug), the $this->data is always empty and uninitialized...  where am I
> >> going wrong?
>
> >> thanks in advance
> >>         Lorenzo
>
> --
> Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
> HOME:http://www.lorenzobettini.itMUSIC:http://www.purplesucker.com
> BLOGS:http://tronprog.blogspot.com http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: testing a controller's add action

2009-12-27 Thread Lorenzo Bettini
Yes, it uses a redirect (inside the method), but the problem comes up 
before reaching a possible redirect: empty($this->data) is true when 
entering the method itself...

As for the redirect I think I already handle that with

class TestPapers extends PapersController {
var $autoRender = false;

 function redirect($url, $status = null, $exit = true) {
 $this->redirectUrl = $url;
 }

but the problem, as I said, it's that no data is passed to the action...

John Andersen wrote:
> Is your add action using a redirect?
>John
> 
> On Dec 26, 6:48 pm, Lorenzo Bettini  wrote:
>> Hi
>>
>> I already managed to test some actions of controllers using testAction
>> method; these were actions that were expected to return something.
>>
>> Now, I'd like to test an add action of the controller, thus, following
>> the book I did:
>>
>> $data = array(
>> 'Paper' => array(
>> 'title' => 'MyTitle',
>> 'year' => 2009
>> ));
>> debug($data);
>> $results = $this->testAction(
>> array(
>> 'controller' => 'papers',
>> 'action' => 'admin_add',
>> ),
>> array('data' => $data, 'method' => 'post')
>> );
>>
>> but when executing the controller's action (I tried that with the
>> debug), the $this->data is always empty and uninitialized...  where am I
>> going wrong?
>>
>> thanks in advance
>> Lorenzo



-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: testing a controller's add action

2009-12-26 Thread John Andersen
Is your add action using a redirect?
   John

On Dec 26, 6:48 pm, Lorenzo Bettini  wrote:
> Hi
>
> I already managed to test some actions of controllers using testAction
> method; these were actions that were expected to return something.
>
> Now, I'd like to test an add action of the controller, thus, following
> the book I did:
>
> $data = array(
>         'Paper' => array(
>                 'title' => 'MyTitle',
>                 'year' => 2009
>         ));
> debug($data);
> $results = $this->testAction(
>         array(
>         'controller' => 'papers',
>         'action' => 'admin_add',
>         ),
>         array('data' => $data, 'method' => 'post')
>         );
>
> but when executing the controller's action (I tried that with the
> debug), the $this->data is always empty and uninitialized...  where am I
> going wrong?
>
> thanks in advance
>         Lorenzo
>
> --
> Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
> HOME:http://www.lorenzobettini.itMUSIC:http://www.purplesucker.com
> BLOGS:http://tronprog.blogspot.com http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


testing a controller's add action

2009-12-26 Thread Lorenzo Bettini
Hi

I already managed to test some actions of controllers using testAction 
method; these were actions that were expected to return something.

Now, I'd like to test an add action of the controller, thus, following 
the book I did:

$data = array(
'Paper' => array(
'title' => 'MyTitle',
'year' => 2009
));
debug($data);
$results = $this->testAction(
array(
'controller' => 'papers',
'action' => 'admin_add',
),
array('data' => $data, 'method' => 'post')
);

but when executing the controller's action (I tried that with the 
debug), the $this->data is always empty and uninitialized...  where am I 
going wrong?

thanks in advance
Lorenzo

-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en