Sunday, July 24, 2011

Undefined method when testing Controllers

Hi,

I'm writing a test for the add() method of a controller using
testAction(). I'm configuring it to simulate a post method. However,
when I run the test, and it gets to the add() method, $this->request-
>is('post') is undefined. It is defined though if I add a user
normally (I mean through the browser, not the testing framework). The
initial code was created with the cake bake tool.

Is this a bug, or am I missing something? Any help is greatly
appreciated!

Here's the relevant piece of code from the controller:


class UsersController extends AppController
{

public function add()
{
debug('Request is post?: '.$this->request->is('post'));
if ($this->request->is('post'))
// Create user
}
}
...
}

And here's the testing code:


class UsersControllerTestCase extends ControllerTestCase
{

public function setUp()
{
parent::setUp();
$this->Users = new UsersController();
$this->Users->constructClasses();
}

public function testAdd()
{
$email = 'someemail@gmail.com';
$password = 'nicePassword';
$hashedPassword = AuthComponent::password($password);

$data = array(
'User' => array(
'email' => $email,
'password' => $password,
),
);

$this->Users = $this->generate('Users', array('components' =>
array('Session')));
// Note: I also tried not creating Session component, same
results.

$result = $this->testAction(
'/users/add',
array(
'method' => 'post',
'return' => 'result',
'data' => $data,
)
);
$result = $this->Users->User->read();

// Verify that the data was added correctly.
$this->assertEqual($email, $result['User']['email']);
$this->assertEqual($hashedPassword, $result['User']
['password']);
}
}

--
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: