的CakePHP&QUOT与......;在模拟对象的方法不起作用 [英] CakePHP "with" method in mock object don't Work

查看:98
本文介绍了的CakePHP&QUOT与......;在模拟对象的方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我控制器的某个动作,我需要验证对象的一个​​信息使用CakePHP 2.2 RC1我的应用程序,在我的测试我已经创造了验证组件的模仿对象,但是当我调用该方法我的模仿对象变得无效,当我没有把这个一切工作正常。

下面的模拟对象至极不工作

  $这个 - >&控制器 -  GT;验证
     - > staticExpects($这个 - >的())
     - >的方法(用户)
     - >在('count_id')
     - >将($这个 - >的returnValue(9));

感谢您的关注球员。

-

修改

我上面的测试案例的全部code,这是一个非常简单的测试。

 类TagsControllerTest扩展ControllerTestCase {
    公共职能testView(){
        $标签= $这个 - >生成('标签',阵列(
            '组件'= GT;阵列(
                会话,
                '验证'=>阵列('用户')
            )
        ));
        $ Tags-> Auth-> staticExpects($这个 - >的())
             - >的方法(用户)
             - >在('count_id')
             - >将($这个 - >的returnValue(2));        $结果= $这个 - > testAction('/标签/视图');
        $这个 - >的assertEquals($结果,2);
    }
}

和我在标记控制器动作的code,这没有什么更多的(用于测试目的)他们的用户对象与count_id作为参数的回报。

 公共职能视图(){
    返回$这个 - > Auth->用户('count_id');
}

运行测试,我收到这样的信息:


  调用零次或多次当

期望失败的方法名等于
  对于调用AuthComponent参数0 ::用户(空)不匹配,期望的价值。
  失败的断言空场预期count_id。



解决方案

在看 AuthComponent code之后,我觉得问题在于,你又不是嘲弄整个组件的的你没有嘲讽 _getUser()方法。不要忘了:你没有嘲讽方法的真正的方法

如果你看一下code你会看到用户()称为_getUser()这又是由启动()。

有两种方法可以解决这个问题,首先是要嘲笑整个 AuthComponent

  $标签= $这个 - >生成('标签',阵列(
        '组件'= GT;阵列(
            会话,
            '验证'/ *没有方法* /
        )
    ));

或模拟 _getUser()除了用户()

  $标签= $这个 - >生成('标签',阵列(
        '组件'= GT;阵列(
            会话,
            '验证'=>阵列('用户','_getUser')
        )
    ));

我们希望,这应该解决您的问题。

I'm trying testing my application using CakePHP 2.2 RC1, in the certain action of my controller i need one information of Auth object, in my test i have created an mock object for the Auth component, but when i call the method with my mock object become invalid, when i don't put this everything works fine.

Below the mock object wich dont work

$this->controller->Auth
    ->staticExpects($this->any())
    ->method('user')
    ->with('count_id')
    ->will($this->returnValue(9));

Thanks for your attention guys.

--

Edit

Above the full code of my test case, this a very simple test.

class TagsControllerTest extends ControllerTestCase {
    public function testView(){
        $Tags = $this->generate('Tags', array(
            'components' => array(
                'Session',
                'Auth' => array('user')
            )
        ));
        $Tags->Auth->staticExpects($this->any())
            ->method('user')
            ->with('count_id')
            ->will($this->returnValue(2));

        $result = $this->testAction('/tags/view');
        $this->assertEquals($result, 2);
    }
}

And the code of my action in the Tag controller, this don't have nothing more (for testing purposes) them a return of user object with count_id as parameter.

public function view(){
    return $this->Auth->user('count_id');
}

Running the test I received this message:

Expectation failed for method name is equal to when invoked zero or more times Parameter 0 for invocation AuthComponent::user(null) does not match expected value. Failed asserting that null matches expected 'count_id'.

解决方案

After looking at the AuthComponent code, I think the problem lies in the fact that you're not mocking the entire component or you're not mocking the _getUser() method. Don't forget: the methods you're not mocking are real methods!

If you look at the code you'll see that user() is called by _getUser() which in turn is called by startup().

There are two ways to solve this problem, the first is to mock the entire AuthComponent:

$Tags = $this->generate('Tags', array(
        'components' => array(
            'Session',
            'Auth' /* no methods */
        )
    ));

or mock _getUser() in addition to user():

$Tags = $this->generate('Tags', array(
        'components' => array(
            'Session',
            'Auth' => array('user', '_getUser')
        )
    ));

Hopefully, this should solve your problem.

这篇关于的CakePHP&QUOT与......;在模拟对象的方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆