Wednesday, September 29, 2010

Re: Model validates when needed field is not available in $data array!

Oh, I found the problem. Because I used the fixture with the ID 1, the
record was already available in the test-DB and so actually there was
a value of number when no number key was in the array. :-) Thanks
anyway.

On Wed, Sep 29, 2010 at 12:02 PM, psybear83 <psybear83@gmail.com> wrote:
> Hi everybody
>
> I'm doing some unit testing right now, and I stumbled over the
> following...
>
> I have a model Contract, which has the following validation rule:
>
> class Contract extends AppModel {
>        var $name = 'Contract';
>        var $validate = array(
>                'number' => array('rule' => '/^[0-9]{3}-[0-9]{2}-[0-9]{3}$/i',
> 'message' => 'must correspond to the pattern "123-45-678"'),
>        );
> }
>
> My test looks the following:
>
>    // 1st
>    $this->Contract->set(array(
>                        'id' => 1,
>                        'number' => '735-10-001',
>                ));
>    $this->assertTrue($this->Contract->validates()); // Passes
>
>    // 2nd
>    $this->Contract->set(array(
>                        'id' => 1,
>                        /*'number' => '735-10-001',*/
>                ));
>    $this->assertTrue($this->Contract->validates()); // Passes
>
>    // 3rd
>    $this->Contract->set(array(
>                        'id' => 1,
>                        'number' => '',
>                ));
>    $this->assertTrue($this->Contract->validates()); // Fails
>
> This seems quite odd to me! Why should the 2nd test pass and the 3rd
> not? IMHO both the 2nd and the 3rd should fail...?
>
> What do you think? Thanks,
> Josh
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: