CakePHP测试,模拟AuthComponent

时间:2014-04-05 22:54:47

标签: unit-testing cakephp

我是单位测试的新手,我的意思是我对了解它。很自然地,我去读了一篇一堆的教程,指南等等......我理解了很多,但我仍然坚持嘲笑AuthComponent。这是我的代码(注释行表示我相信它的作用,随意纠正我)

// 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));

我不确定如果我应该为$this->Auth->user($param)的每次调用写一个块,或者我该怎么做,但是我收到与传递给{{1}的参数有关的错误},

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

0 个答案:

没有答案