Unit testing 如何测试需要身份验证的操作?

Unit testing 如何测试需要身份验证的操作?,unit-testing,authentication,cakephp,phpunit,cakephp-3.0,Unit Testing,Authentication,Cakephp,Phpunit,Cakephp 3.0,我正在尝试测试BookController的add方法。条件是用户需要在添加书籍之前登录。但是我没有找到任何方法让用户在测试方法中登录 我的代码是这样的- public function testAdd() { $user = ['email' => 'admin@example.com', 'password' => 'abcd']; $this->post('/users/login', $user); $book

我正在尝试测试BookController的add方法。条件是用户需要在添加书籍之前登录。但是我没有找到任何方法让用户在测试方法中登录

我的代码是这样的-

 public function testAdd()
    {
        $user = ['email' => 'admin@example.com', 'password' => 'abcd'];
        $this->post('/users/login', $user);
        $book = ['title' => 'Foo', 'writer' => 'writer1', 'edition' => '2nd', 'course' => 'CSE', 'description' => 'abcd', 'price' => '200', 'status' => '0', 'user_id' => '1', 'photo' => 'abcd'];
        $this->post('/books/add', $book);
        $this->assertRedirect('/books');
}
//Login Function
    public function login()
    {
        if($this->request->session()->read('Auth.User'))
        {
            $this->Flash->error(__('You are already logged in.'));
            return $this->redirect(['controller' => 'home','action' => 'index']);
        }
        if($this->request->is('post'))
        {
               $user = $this->Auth->identify();
               if($user)
               {
                   $this->Auth->setUser($user);
                   return $this->redirect($this->Auth->redirectUrl());
               }
        }

        //In case of bad login
        $this->Flash->error('You must login first.');
    }
断言失败,因为我被重定向到/users/login

我的登录方式是这样的-

 public function testAdd()
    {
        $user = ['email' => 'admin@example.com', 'password' => 'abcd'];
        $this->post('/users/login', $user);
        $book = ['title' => 'Foo', 'writer' => 'writer1', 'edition' => '2nd', 'course' => 'CSE', 'description' => 'abcd', 'price' => '200', 'status' => '0', 'user_id' => '1', 'photo' => 'abcd'];
        $this->post('/books/add', $book);
        $this->assertRedirect('/books');
}
//Login Function
    public function login()
    {
        if($this->request->session()->read('Auth.User'))
        {
            $this->Flash->error(__('You are already logged in.'));
            return $this->redirect(['controller' => 'home','action' => 'index']);
        }
        if($this->request->is('post'))
        {
               $user = $this->Auth->identify();
               if($user)
               {
                   $this->Auth->setUser($user);
                   return $this->redirect($this->Auth->redirectUrl());
               }
        }

        //In case of bad login
        $this->Flash->error('You must login first.');
    }
有没有办法解决这个问题?
提前谢谢

集成测试不是这样工作的,您不应该在一个测试方法中发出多个请求,这很容易导致污染,因为会话数据、cookie、令牌配置等仅在测试方法运行后重置,而不是在两个请求之间重置

这就是说,模拟登录用户只需将适当的身份验证信息添加到相应的存储或请求中即可。如果您正在使用会话存储,只需在向
add
操作发出请求之前将信息添加到会话:

$this->session([
    'Auth' => [
        'User' => [
            'id' => 1,
            'username' => 'foo'
            // ...
        ]
    ]
]);
另见


集成测试不是这样工作的,您不应该在一个测试方法中发出多个请求,这很容易导致污染,因为会话数据、cookie、令牌配置等仅在测试方法运行后重置,而不是在两个请求之间重置

这就是说,模拟登录用户只需将适当的身份验证信息添加到相应的存储或请求中即可。如果您正在使用会话存储,只需在向
add
操作发出请求之前将信息添加到会话:

$this->session([
    'Auth' => [
        'User' => [
            'id' => 1,
            'username' => 'foo'
            // ...
        ]
    ]
]);
另见


谢谢!你搞定了!谢谢你搞定了!