【问题标题】:Cakephp 1.3 mock authenticationCakephp 1.3 模拟认证
【发布时间】:2013-01-04 17:32:58
【问题描述】:

我正在尝试编写和测试控制器操作。这是我所拥有的:

App::import('Component', 'Auth');
App::import('Controller', 'Goals');

class GoalControllerTest extends CakeTestCase {

    function startCase() {
            echo '<h1>Starting Test Case</h1>';
            $this->Goals = new TestGoalsController();
            $this->Goals->constructClasses();
            $this->Goals->Component->initialize($this->Goals);
    }

    function endCase() {
            unset($this->Goals);
            ClassRegistry::flush();
            echo '<h1>Ending Test Case</h1>';
    }

    function startTest($method) {
            echo '<h3>Starting method ' . $method . '</h3>';
            //$this->GoalsController = new TestGoalsController();
            Mock::generate('AuthComponent');
            $this->Goals->Auth = new MockAuthComponent();
    }

    function endTest($method) {
            echo '<hr />';
    }

    function testSetHomepage() {
            //get goal for test
            $sql = "SELECT id FROM goals limit 1";
            $goal = $this->Goals->Goal->query($sql);
            $this->Goals->params = Router::parse('/goals/setHomepage/');
            $this->Goals->beforeFilter();
            $this->Goals->Component->startup($this->Goals);
            $this->Goals->params['url']['goal_id'] = $goal[0]['goals']['id'];
            $this->Goals->params['url']['set_to'] = 1;
            $this->Goals->setHomepage();

            //Mock Auth
            $this->Goals->Auth->setReturnValue('user', 1);


            //check the set
            $sql = "SELECT GoalOnHome.goal_id FROM goals_users as GoalOnHome WHERE GoalOnHome.goal_id = '" . $this->Goals->params['url']['goal_id'] . "' limit 1";
            $result = $this->Goals->Goal->query($sql);

            $expected = false;
            $this->assertEqual(empty($result), $expected);
            unset($this->Goals->params['url']);
    }
}

我正在尝试模拟身份验证组件,但在运行测试时出现此错误:

Unexpected PHP error [Undefined property: MockAuthComponent::$enabled] severity [E_NOTICE] in

有人可以帮我解决我在这里做错了什么吗?

【问题讨论】:

  • 我摆脱了未定义的属性错误,将模拟生成移动到:function startCase() { echo '

    Starting Test Case

    '; $this->Goals = new TestGoalsController();模拟::generate('AuthComponent'); $this->Goals->Auth = new MockAuthComponent(); $this->Goals->constructClasses(); $this->Goals->组件->初始化($this->Goals);但我知道收到此错误:致命错误:调用未定义的方法 AuthComponent::setReturnValue() in

标签: mocking cakephp-1.3 simpletest


【解决方案1】:

我最近遇到了同样的问题。我通过简单地在 Mock 对象上设置属性来修复它。此外,我为startup-方法配置了一个返回值——否则,模拟身份验证将不起作用。

function startTest() {
    // Set up controller etc.
    // ...
    $this->Goals->Auth = new MockAuthComponent();  
    $this->Goals->Auth->enabled = true; // Should solve your error
    $this->Goals->Auth->setReturnValue('startup', true);
}

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 2011-02-02
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多