Unit testing CakePHP测试、模拟组件

Unit testing CakePHP测试、模拟组件,unit-testing,cakephp,Unit Testing,Cakephp,我是单元测试新手,我的意思是我对单元测试的知识为零。因此,我自然地去读了一堆教程、指南等。。。我理解了很多,但我仍然对模仿AuthComponent感到厌烦。这是我的代码(注释行表示我相信它的作用,请随意更正) 我不确定是否应该为$this->Auth->user($param)的每次调用编写一个块,或者我应该如何编写,但是我得到了与传递给Auth->user()的参数相关的错误 我的问题是,如果我的真实代码多次调用Auth->user('id'),那么在ControllerTestCase中模

我是单元测试新手,我的意思是我对单元测试的知识为零。因此,我自然地去读了一堆教程、指南等。。。我理解了很多,但我仍然对模仿
AuthComponent
感到厌烦。这是我的代码(注释行表示我相信它的作用,请随意更正)

我不确定是否应该为
$this->Auth->user($param)
的每次调用编写一个块,或者我应该如何编写,但是我得到了与传递给
Auth->user()的参数相关的错误


我的问题是,如果我的真实代码多次调用
Auth->user('id')
,那么在
ControllerTestCase
中模拟它的正确方法是什么?

您可能正在寻找,但这取决于您正在测试的代码……如果我能看到一个示例,我相信我会更了解它)我也遇到过类似的问题,但被接受的答案对我起了作用。
// The testing is generating a Controller
$Students = $this->generate('Student', array(
    'components' => array(
        'Auth' => array('user')
    )
));

$Students->Auth->expects($this->once())   // The testing is expecting to be called once
    ->method('user')                    // when calling $this->Auth->user();
    ->will($this->returnValue(array(    // return the following array
        'id' => 1,
        'role' => 'Administrator'
    )));

$Students->Auth->expects($this->once())   // The testing is expecting to be called once
    ->method('user')                    // when calling $this->Auth->user('id);
    ->with('id')                        // returns 1
    ->will($this->returnValue(array(1));