我想我在這裏問一些幫助我的問題。我花了整整一晚上。我有這樣的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