私は100%のコードカバレッジを達成しようとしているが、私は以下のような行をカバーする方法を判断することはできません。それは単に終わりです}。私は、これらの行を私のコード全体に加えて、else {などの行にも書いていますが、それらはカバーされておらず、デッドコードとしてマークされていません。どのように私はクラック、サンプルを提供するコード行をカバーするためにテストを書くだろう
これらの行をカバーするテストを作成するにはどうすればよいですか?
34 : public function addAction() {
35 :
36 2 : $form = new Roles_Form_Add();
37 :
38 2 : if ($this->getRequest()->isPost()) {
39 :
40 1 : if ($form->isValid($this->getRequest()->getPost())) {
41 :
42 1 : $clean = $form->getValues();
43 :
44 1 : $roleService = new Roles_Service_Role();
45 :
46 1 : $role = $roleService->fetchNew();
47 1 : $role->setFromArray($clean)
48 1 : ->save();
49 :
50 1 : $this->_helper->flashMessenger('A new role has been added.');
51 :
52 1 : $this->_helper->redirector('view','role','roles',array('id'=>$role->id));
53 1 : return;
54 :
55 : }
56 :
57 0 : }
58 :
59 1 : $form->setAction($this->_helper->url('add','role','roles'));
60 :
61 1 : $this->view->addRoleForm = $form;
62 :
63 1 : }
64 :
public function testAddAction() {
$this->dispatch('/roles/role/add');
$this->assertModule('roles');
$this->assertController('role');
$this->assertAction('add');
$this->assertQuery('form#addRole input#name');
}
public function testAddActionWithPost() {
$this->getRequest()->setMethod('POST')
->setPost('name','Test');
$this->dispatch('/roles/role/add');
$this->assertRedirectTo('/roles/role/view/id/1');
}
あなたのコードカバレッジツールは、あなたがポジティブな条件をテストしたが、否定的な条件をテストしていないことを示唆しているかもしれません(38行目のブロックが実行されていない場合、 isPost()がfalseを返すテストを作成してみてください。 –
私は投稿なしのテストをしていますが、私は使用されたテストを追加しました。その他のアドバイスは? – Sosy