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

Reply via email to