在CakePHP中,我写了两个模型,一个是Invoice,另一个是Product。发票“hasMany”与Product关联。发票有几个验证集,如“validateCreateInvoice”和“validateUpdateInvoice”等......
我正在编写如下的PHPUnit测试用例来对测试集进行单元测试。那么如何1)动态设置验证集? 2)从PHPUnit验证模型和相关模型?
我运行了以下代码,商家电子邮件和产品名称数据验证失败。但事实并非如此。错误转储为空。可能是什么问题呢 ? 注意:验证规则通过模型保存方法完美地工作,这意味着验证规则没有问题。
App::import('Model', 'Invoice');
App::import('Model', 'Product');
App::uses('Validation', 'Utility');
App::uses('CakeFixtureManager', 'TestSuite/Fixture');
App::uses('CakeTestFixture', 'TestSuite/Fixture');
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Autoload.php';
class InvoiceTest extends PHPUnit_Framework_TestCase {
public $sourceName = 'Invoice';
public function setUp() {
parent::setUp();
$this->Invoice=& ClassRegistry::init('Invoice');
$this->InvoiceValidation =& ClassRegistry::init('Validate');
}
public function tearDown() {
parent::tearDown();
unset($this->Model, $this->Source);
ConnectionManager::drop($this->sourceName);
}
public function testCreate() {
$data = array(
"merchantEmail" => "stest_1339339519_bizyahoo.com", // not a valid email address
"payerEmail" => "stest_1340973819_per@yahoo.com",
"currencyCode" => "USD",
"paymentTerms" => "Net10",
"Products" =>array(
0 => array (
"productName" => "", // product name shouldn't be empty
"description" => "From CocaCola",
"unitPrice" => 1.56,
"quantity" => 1,
"taxName" => "Tax1",
"taxRate" => 7
),
1 => array (
"productName" => "Pepsi1",
"description" => "From Pepsi",
"unitPrice" => 1.65,
"quantity" => 1,
"taxName" => "Tax2",
"taxRate" => 7
),
),
);
//Create Invoice
$invoice=new Invoice();
$invoice->set($data);
$this->Invoice->setValidation('validateCreateInvoice');
$errors = $this->Invoice->validates($data); // validation should fails because of invalid merchant email address
Debugger::dump($errors); // output is null
$errors1 = $this->Invoice->validateAssociated($data); // validation should fail because product name is empty
Debugger::dump($errors1); // output is null
}
}
答案 0 :(得分:0)
您似乎使用面包店的非核心多重验证行为,测试套件似乎不尊重行为
我会抛弃多重验证行为,转而使用std验证的标准'on'属性: http://book.cakephp.org/2.0/en/models/data-validation.html#on