ここで私の問題に対する助けを求めなければならないと思いました。私はこれで一晩中過ごしました。UsersController
次のようなログイン方法があります。
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)
何が原因なのかわかりません... テストメソッドのどこが間違っているのでしょうか?どのように記述すればよいのでしょうか?
ありがとう!