Monday, December 28, 2009

Re: testing a controller's add action

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 <bett...@dsi.unifi.it> 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 <bett...@dsi.unifi.it> 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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: