2016-10-17 66 views
0

我一直在嘗試cakephp 3,因爲昨天。我有興趣使用單元測試。測試buildRules cakephp

我已成功測試我的添加動作。但是,如果存在一些驗證錯誤,我在跟蹤時遇到困難。我怎樣才能測試testDefaultValidation()和buildRules(),因爲它們的內容只有$this->markTestIncomplete('Not implemented yet.'); ??

謝謝。

+0

如果您提供的驗證不是空的,那麼當您測試添加時,不應該保存空值,等等。 –

回答

1

您可以使用表格中提供的方法checkRules編寫單元測試,請參閱http://api.cakephp.org/3.3/source-class-Cake.Datasource.RulesAwareTrait.html#40-80

舉個例子,如果你有這樣的要求,從articlesid存在以下將考驗你不得不設置正確buildRules一個comments表。

$table = TableRegistry::get('Comments'); 

// Where an row in the articles table with id 123 doesn't exist 
$comment = $table->newEntity([ 
    'article_id' => 123 
]); 

$result = $table->checkRules($comment); 
$this->assertFalse($result); 

$expected = [ 
    'article_id' => [ 
     '_existsIn' => 'This value does not exist' 
    ] 
]; 
$this->assertEquals($expected, $comment->errors()); 
+0

謝謝。你搖滾!順便說一句,這裏是代碼的適當位置? 'PatientsTableTest.php'或'PatientsControllerTest.php'?或者它也是一樣的,因爲我可以在'PatientsTableTest'或'PatientsController'中放置並運行它,沒問題! – thom

+0

沒問題!你可以把它放在任何一個,但是當你測試一個表格方法時,我會把它放在'PatientsTableTest'中。 – cjquinn

+0

羅傑那..非常感謝:) – thom