Skip to content

Instantly share code, notes, and snippets.

@dai199
Created January 29, 2013 02:43
Show Gist options
  • Select an option

  • Save dai199/4661275 to your computer and use it in GitHub Desktop.

Select an option

Save dai199/4661275 to your computer and use it in GitHub Desktop.
CakePHP2でmodelのバリデーションテストを行う
<?php
// app/Model/User.php
class User extends AppModel {
public $validate = array(
'email' => 'email'
);
}
<?php
// app/Test/Case/Model/UserTest.php
App::uses('User', 'Model');
class UserTest extends CakeTestCase {
public $fixtures = array(
'app.user'
);
public function setUp(){
parent::setUp();
$this->User = ClassRegistry::init('User');
}
public function tearDown(){
unset($this->User);
parent::tearDown();
}
public function testValidation(){
$data = array('User' =>
array(
'email' => 'test' // error
)
);
$this->User->create($data);
$result = $this->User->validates(); // true if there are no errors
$this->assertFalse($result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment