CakePHP使用映射方法测试行为 [英] CakePHP test behaviour with mapped method

查看:66
本文介绍了CakePHP使用映射方法测试行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建 OwnableBehavior 时,我决定使用可用的 $ mapMethods 属性。它将任何名为 isOwnedByXXX()的方法映射到 isOwnedBy()(此文档的链接为< a href = http://book.cakephp.org/2.0/en/models/behaviors.html#mapped-methods rel = nofollow>此处)

Whilst creating an OwnableBehavior I decided to use the $mapMethods property that is available. It is to map any method called isOwnedByXXX() to isOwnedBy() (The link for the documentation on this is here)

这是我的 OwnableBehavior 代码:

class OwnableBehavior extends Model Behavior {

    public $mapMethods = array('/isOwnedBy(\w+)/' => 'isOwnedBy');

    public function isOwnedBy(Model $model, $type, $id, Model $userModel, $userId) {
         // Method is currently empty
    }
}

这是TestCase代码:

Here is the TestCase code:

class OwnableBehaviorTest extends CakeTestCase {

    public function testIsOwned() {
        $TestModel = new Avatar();
        $TestModel->Behaviors->attach('Ownable');
        $result = $TestModel->Behaviors->Ownable->isOwnedByUser(
            $TestModel, 1, new User(), 1);
        $this->assertTrue($result);
    }
}

运行测试时出现此错误:

When I run the test I get this error:

 Call to undefined method OwnableBehavior::isOwnedByUser()

如果我将方法调用更改为 isOwnedBy($ TestModel,'user',1,new User(),1); 这行得通,因此由于某种原因看起来映射的方法在单元测试期间不起作用。我已经在控制器中测试了映射的方法,但是没有任何错误。

If I change the method call to isOwnedBy($TestModel, 'user', 1, new User(), 1); this works, so it looks like for some reason the mapped methods aren't working during the unit test. I have tested the mapped methods in a controller and I get no errors.

我想知道是否取决于将行为加载到模型中的原因。我在食谱中找不到有关如何正确测试行为的文档,例如组件,助手等。因此,我只是使用了核心行为测试所使用的相同技术(位于 Cake / Test / Case / Model / Behavior / )。

I wondered if it was down to how I was loading the behaviour into the model. I couldn't find any documentation in the cookbook on how to properly test Behaviours like there is with Components, Helpers, etc... So I just used the same techniques that the Core Behaviour tests use (Found in Cake/Test/Case/Model/Behavior/).

我确实认为可能是因为我覆盖了这一事实 ModelBehavior :: setup()方法,但是我尝试添加 parent :: setup($ model,$ settings)在安装方法的开始,我仍然遇到相同的错误。我不会覆盖其他任何 ModelBehavior 方法。

I did think maybe it could have been down to the fact that I am overwriting the ModelBehavior::setup() method, but I tried adding parent::setup($model, $settings) at the start of the setup method and I still get the same error. I am not overwriting any of the other ModelBehavior methods.

我想我可以只使用 OwnableBehavior :: isOwnedBy()方法,但是我很想知道是否可以在单元测试期间使映射的方法正常工作。

I guess I could just use the OwnableBehavior::isOwnedBy() method, but I'd quite like to know if I could get the mapped methods to work during a unit test.

推荐答案

我找到的解决方案正在替换此行:

The solution I have found is replacing this line:

$result = $TestModel->Behaviors->Ownable->isOwnedByUser(...);

具有:

$result = $TestModel->isOwnedByUser(...);

因此,只是像在应用程序中那样使用它,直接调用行为方法从模型。我不知道这是否会破坏单元测试的想法,并将其更多地用于集成测试。

So it's just a case of using it more like you would in the application, calling the behaviour method directly from the model. I don't know if this ruins the idea of a unit test and makes it more into integration testing though.

这篇关于CakePHP使用映射方法测试行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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