CakePHP - 未调用单元测试验证

发布于 2024-11-07 07:02:16 字数 1979 浏览 1 评论 0原文

我正在做一些单元测试,但遇到模型验证未调用的问题。

这是我在夹具中的内容。

<?php
    class ItemFixture extends CakeTestFixture {
    var $name = 'Item';
    var $import = array(
            'model' => 'Item',
            'table' => 'items'
    );

    var $records = array(array(
            'id' => 1,
            'title' => 'Title2',
            'year' => '1992'
            ),array(
            'id' => 2,
            'title' => 'Lorem ipsum dolor sit amet',
            'year' => '1995'
            ));
    }

?>

这是我在模型单元测试中得到的内容

    <?php

    App::import('Model', 'Item');

    class ItemTest extends Item {
            var $name = 'ItemTest';
            var $useTable = 'items';
            var $useDbConfig = 'test';
    }

    class ItemTestCase extends CakeTestCase {
            var $fixtures = array('app.item');

            function startTest() {
                    $this->ItemTest =& ClassRegistry::init('ItemTest');
            }

            function testAdd() {
                    $data = array(
                    'ItemTest' => array(
                    'title' => 'Best article Evar!'
                    )
                    );

                    $result = $this->ItemTest->saveAll($data);
            }

            function endTest() {

    }

}

?>

这是我在实际模型中进行的验证。

    'year' => array(
            'numeric' => array(
            'rule' => 'numeric',
            'allowEmpty' => false,
            'message' => 'Numbers only'
    ),
    'minLength' => array(
            'rule' => array('minLength', 4),
            'message' => 'Year in YYYY format'
    ),
    'maxLength' => array(
            'rule' => array('maxLength', 4),
            'message' => 'Year in YYYY format'
    )

看,在 testAdd 中,我没有通过年份,它不应该为空,但它实际上已经过去了。为什么没有调用验证?

谢谢你,

蒂伊

I'm doing some unit testing but having issues with Model validation not getting called.

Here is what I have in the Fixture.

<?php
    class ItemFixture extends CakeTestFixture {
    var $name = 'Item';
    var $import = array(
            'model' => 'Item',
            'table' => 'items'
    );

    var $records = array(array(
            'id' => 1,
            'title' => 'Title2',
            'year' => '1992'
            ),array(
            'id' => 2,
            'title' => 'Lorem ipsum dolor sit amet',
            'year' => '1995'
            ));
    }

?>

Here is what I have in the model unit test

    <?php

    App::import('Model', 'Item');

    class ItemTest extends Item {
            var $name = 'ItemTest';
            var $useTable = 'items';
            var $useDbConfig = 'test';
    }

    class ItemTestCase extends CakeTestCase {
            var $fixtures = array('app.item');

            function startTest() {
                    $this->ItemTest =& ClassRegistry::init('ItemTest');
            }

            function testAdd() {
                    $data = array(
                    'ItemTest' => array(
                    'title' => 'Best article Evar!'
                    )
                    );

                    $result = $this->ItemTest->saveAll($data);
            }

            function endTest() {

    }

}

?>

And here is the validation that I have in the actual Model.

    'year' => array(
            'numeric' => array(
            'rule' => 'numeric',
            'allowEmpty' => false,
            'message' => 'Numbers only'
    ),
    'minLength' => array(
            'rule' => array('minLength', 4),
            'message' => 'Year in YYYY format'
    ),
    'maxLength' => array(
            'rule' => array('maxLength', 4),
            'message' => 'Year in YYYY format'
    )

See, in testAdd, I'm not passing year which is not supposed to be empty, but it actually passed. Why isn't the validation getting called?

Thank you,

Tee

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-11-14 07:02:16

allowEmpty 仅在数据包含指定字段(在您的情况下为 year)时才有效。要使验证失败,您必须添加 'year' => ''testAdd 中的 $data 数组或添加 'required' =>; true 到您的验证规则以强制数据中存在year 字段。

另请参阅http://book.cakephp.org/view/1148/allowEmpty

allowEmpty only works if the data contains the specified field, in your case year. To make the validation fail you either have to add 'year' => '' to your $data array in testAdd or add 'required' => true to your validation rule to enforce there is a year field in the data.

See also http://book.cakephp.org/view/1148/allowEmpty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文