我想我在这里问一些帮助我的问题。我花了整整一晚上。我有这样的UsersController
登录方法:CakePHP 2.3 - 单元测试用户登录
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'reservations', 'action' => 'index'));
} else {
$this->Session->setFlash(__('Login error.'), 'flashError');
}
}
}
我试图用PHPUnit的来测试这一点,这样我就可以确保只有合法的用户登录成功,他们将被重定向到一个特定的后登录→页。这里是我的testLogin
方法UsersControllerTest
类:
function testLogin() {
$UsersController = $this->generate('Users', array(
'components' => array(
'Auth' => array('user')
),
)
);
$UsersController->Auth->expects($this->any())
->method('user')
->with('id')
->will($this->returnValue(2));
$data = array('User' => array(
'student_number' => 1111111,
'password' => 'qwerty'
));
//$UsersController->Auth->login($data['User']);
$this->testAction('/users/login', array('data' => $data, 'method' => 'get'));
$url = parse_url($this->headers['Location']);
$this->assertEquals($url['path'], '/reservations');
}
我仍然在学习与CakePHP的单元测试的基础知识。我得到这个错误:
PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)
我不知道是什么原因导致这...这有什么错我的测试方法,它应该怎么写?
谢谢!
'$ this-> headers'没有'Location'键。对于这个问题...... $ this-> headers来自哪里? –
也许你正在寻找'响应'标题? [CakeResponse - 设置标题](http://book.cakephp.org/2.0/en/controllers/request-response.html#setting-headers) – thaJeztah