Getting viewVars set in a controller when using Mock objects during testing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Getting viewVars set in a controller when using Mock objects during testing

wratke
I'm wondering if there's any way to get the viewVars when unit testing
a
controller without using the testAction. I've taken a look at the
testAction function in the Cake code and see how it gets the View
object
and then dumps and returns the viewVars, but as far as I can tell
there
isn't any way to do something similar without using the testAction
function. Is there something in Cake that allows you to access the
viewVars that I'm missing? Or should I be dumping the viewVars from
the
View object manually in my tests? I'm excited to start testing my
controllers with Mock objects, but without having access to the
viewVars
set in the controller I'm finding it difficult to make any assertions
in
my tests to make sure things are being set properly.

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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Getting viewVars set in a controller when using Mock objects during testing

jacmoe
Here's how I do this:
        $view =& ClassRegistry::getObject('view');
        $some_view_var = isset($view->viewVars['var']
['another_var']) ? $view->viewVars['var']['another_var'] : null;
I am aware that it is against CakePHP coding conventions to use the
tertiary conditional operator, but I don't care. :P

Cheers

Jacob

On Apr 25, 4:08 pm, wratke <[hidden email]> wrote:

> I'm wondering if there's any way to get the viewVars when unit testing
> a
> controller without using the testAction. I've taken a look at the
> testAction function in the Cake code and see how it gets the View
> object
> and then dumps and returns the viewVars, but as far as I can tell
> there
> isn't any way to do something similar without using the testAction
> function. Is there something in Cake that allows you to access the
> viewVars that I'm missing? Or should I be dumping the viewVars from
> the
> View object manually in my tests? I'm excited to start testing my
> controllers with Mock objects, but without having access to the
> viewVars
> set in the controller I'm finding it difficult to make any assertions
> in
> my tests to make sure things are being set properly.
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd 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 [hidden email]
> To unsubscribe from this group, send email to
> [hidden email] For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en

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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Getting viewVars set in a controller when using Mock objects during testing

calvin-6
Thanks for that snippet. I've been looking for a way to grab view
variables from outside of the view as well. I was trying to figure out
how the form helper does it (the way select options are automagically
pulled from $$associatedModelName) but wasn't able to find the code
where it's done. ClassRegistry is the best way to do this inside of a
helper as well, right?


On Apr 25, 3:31 pm, jacmoe <[hidden email]> wrote:

> Here's how I do this:
>         $view =& ClassRegistry::getObject('view');
>         $some_view_var = isset($view->viewVars['var']
> ['another_var']) ? $view->viewVars['var']['another_var'] : null;
> I am aware that it is against CakePHP coding conventions to use the
> tertiary conditional operator, but I don't care. :P
>
> Cheers
>
> Jacob
>
> On Apr 25, 4:08 pm, wratke <[hidden email]> wrote:
>
>
>
> > I'm wondering if there's any way to get the viewVars when unit testing
> > a
> > controller without using the testAction. I've taken a look at the
> > testAction function in the Cake code and see how it gets the View
> > object
> > and then dumps and returns the viewVars, but as far as I can tell
> > there
> > isn't any way to do something similar without using the testAction
> > function. Is there something in Cake that allows you to access the
> > viewVars that I'm missing? Or should I be dumping the viewVars from
> > the
> > View object manually in my tests? I'm excited to start testing my
> > controllers with Mock objects, but without having access to the
> > viewVars
> > set in the controller I'm finding it difficult to make any assertions
> > in
> > my tests to make sure things are being set properly.
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp 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 [hidden email]
> > To unsubscribe from this group, send email to
> > [hidden email] For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd 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 [hidden email]
> To unsubscribe from this group, send email to
> [hidden email] For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en

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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Getting viewVars set in a controller when using Mock objects during testing

wratke
In reply to this post by jacmoe

I'm still having problems accessing the viewVars for the view object while testing. I fetch the view object using the ClassRegistry but I get a Fatal error: Call to a member function getVars() on a non-object error.

I've included my code below, if anyone has any pointers I'd really appreciate it.

App::import('Controller', 'Students');
App::import('Component', 'Session');
Mock::generate('SessionComponent');


class TestStudentsController extends StudentsController {
    var $name = 'Students';
    var $autoRender = false;
                 
    function redirect($url, $status = null, $exit = true) {
        $this->redirectUrl = $url;
    }
   
     function render($action = null, $layout = null, $file = null) {
        $this->renderedAction = $action;
    }
   
    function _stop($status = 0) {
        $this->stopped = $status;
    }

}

class StudentsControllerTestCase extends CakeTestCase {

    function startTest() {
        $this->Students =& new TestStudentsController();
        $this->Students->constructClasses();
        $this->Students->Component->initialize($this->Students);
    }

    function testIndexWithStudents() {
       
        $this->Students->Session = new MockSessionComponent();
        $this->Students->Session->setReturnValue('read', 2, array('Auth.User.id'));
        $this->Students->index();

        $view =& ClassRegistry::getObject('view');
        $viewVars = $view->getVars();
        debug($viewVars);
       
    }

}

On 4/25/2010 6:31 PM, jacmoe wrote:
Here's how I do this:
        $view =& ClassRegistry::getObject('view');
        $some_view_var = isset($view->viewVars['var']
['another_var']) ? $view->viewVars['var']['another_var'] : null;
I am aware that it is against CakePHP coding conventions to use the
tertiary conditional operator, but I don't care. :P

Cheers

Jacob

On Apr 25, 4:08 pm, wratke [hidden email] wrote:
  
I'm wondering if there's any way to get the viewVars when unit testing
a
controller without using the testAction. I've taken a look at the
testAction function in the Cake code and see how it gets the View
object
and then dumps and returns the viewVars, but as far as I can tell
there
isn't any way to do something similar without using the testAction
function. Is there something in Cake that allows you to access the
viewVars that I'm missing? Or should I be dumping the viewVars from
the
View object manually in my tests? I'm excited to start testing my
controllers with Mock objects, but without having access to the
viewVars
set in the controller I'm finding it difficult to make any assertions
in
my tests to make sure things are being set properly.

Check out the new CakePHP Questions sitehttp://cakeqs.organd 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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en
    
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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

  

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 [hidden email]
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Loading...