Biting the bullet and doing some playing around with model unit testing.

As a bit of a trial, I've got a beforeSave which searches $this for the max
of a field and increments it by 1.

code:

public function beforeSave($options = array()) {    if
(!isset($this->data['number'])) {        $lastNumber =
$this->find('first', [            'fields' => ['id', 'max_number'],
        'conditions' => ['Issue.project_id' =>
$this->data['project_id']]        ]);        $this->data['number'] =
++$lastNumber['Issue']['max_number'];    }    return true;}



max_number is a virtual field described by

public $virtualFields = array(        'max_number' => 'MAX(number)');



My test asserts that the $this->Issue->beforeSave() returns true, and that
$this->Issue->data['number'] is a predictable result:

public function testBeforeSaveShouldIncrementNewIssueNumber() {
$this->loadFixtures('Issue', 'Project', 'IssueType');
$this->Issue->data = [        'project_id' => 1,        'type_id' =>
1,        'short_description' => 'lorem ipsum',        'description'
=> 'Lorem ipsum dolor sit amet',        'created' => '2013-06-07
13:34:20',        'modified' => '2013-06-07 13:34:20',    ];
$this->assertTrue($this->Issue->beforeSave());    // assumes the
fixture has 10 records for project 1    $this->assertEquals(11,
$this->Issue->data['number']);}


I feel that using fixtures here is a little bit dirty - is there a way to
pass mocked data to beforeSave in this instance and remove the need for the
database/fixtures in this instance given all beforeSave() is doing is
adding incrementing the max_number for a Project?

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

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


Reply via email to