4

我一直在看食谱上的一些例子,但我不明白: http ://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example

如何在像这样的删除操作中测试重定向?

public function delete($id = null){         
        $this->Comment->id = $id;
        if (!$this->Comment->exists()) {
            throw new NotFoundException(__('Invalid comment'));
        }
        if ($this->Comment->delete()) {         
            $this->Session->setFlash(__('Comment deleted'));
            return $this->redirect(array('controller' => 'posts', 'action' => 'view', $idPost));
        }
        $this->Session->setFlash(__('Comment was not deleted'));
        return $this->redirect(array('controller' => 'posts', 'action' => 'view', $idPost));        
    }
}

重定向调用后测试停止,因此它甚至不打印此回显:

public function testDelete(){       
    $result = $this->testAction("/comments/delete/1");
    echo "this is not printed";
    print_r($this->headers);        
}
4

4 回答 4

7

测试您的删除操作应该与测试任何其他操作相对相同。您的测试用例可能看起来像这样。

// notice it extends ControllerTestCase
class PostsControllerTest extends ControllerTestCase {

    function testDelete() {
      $this->testAction('/posts/delete/1');
      $results = $this->headers['Location'];
      // your OP code redirected them to a view, which I assume is wrong
      // because the item would be deleted
      $expected = '/posts/index';
      // check redirect
      $this->assertEquals($results, $expected);

      // check that it was deleted
      $this->Posts->Post->id = 1;
      $this->assertFalse($this->Posts->Post->exists());
    }

}

当然,这只是检查显而易见的。您还可以检查会话并编写预期异常的测试。如果它仍然没有达到测试用例的结尾或继续,则其他事情正在发生

您可以使用generateon 方法生成简单的模拟ControllerTestCase

function testDelete() {
  $Posts = $this->generate('Posts', array(
    'components' => array(
      'Email' => array('send'),
      'Session'
    )
  ));
  // set ControllerTestCase to use this mock
  $this->controller = $Posts;

  $this->testAction('/posts/some_action_that_sends_email');
}

以上将首先生成 PostsController 的模拟以在测试期间使用。它还send()模拟了 EmailComponent 和整个 SessionComponent 上的方法。

有关模拟的更多信息:http ://www.phpunit.de/manual/3.0/en/mock-objects.html

有关更多信息generate()http ://book.cakephp.org/2.0/en/development/testing.html#using-mocks-with-testaction

于 2012-04-17T14:46:32.853 回答
1

可能你有一个错误,因为$idPost未定义。

我会写这样的东西:

public function delete($id = null){         
        $this->Comment->id = $id;
        if (!$this->Comment->exists()) {
            throw new NotFoundException(__('Invalid comment'));
        }
        if ($this->Comment->delete()) {         
            $this->Session->setFlash(__('Comment deleted'));
        } else {
            $this->Session->setFlash(__('Comment was not deleted'));
        }
        $this->redirect(array('controller' => 'posts', 'action' => 'view', $id));        
    }
}

并测试它是这样的:

public function testDeleteWithSuccess() {
        $Controller = $this->generate('Comments', array(
            'components' => array(
                'Session'
            ),
            'models' => array(
                'Comment' => array('exists')
            )
        ));

        $Controller->Comment->expects($this->once())
            ->method('exists')
            ->will($this->returnValue(true));

        $Controller->Session->expects($this->once())
            ->method('setFlash')
            ->with('Comment deleted');

        $this->testAction("/comments/delete/ID");

        $this->assertEquals($this->headers['Location'], 'http://'. $_SERVER['HTTP_HOST'] . '/posts/view/ID');
    }
于 2012-04-30T04:38:30.317 回答
0

永远不会打印回声,您的函数“删除”总是在结束前调用重定向。

于 2012-04-17T11:39:40.813 回答
0
public function delete($id = null){         
    $this->Comment->id = $id;
    if (!$this->Comment->exists()) {
        throw new NotFoundException(__('Invalid comment'));
    }
    if ($this->Comment->delete()) {         
        $this->Session->setFlash(__('Comment deleted'));
    } else {
        $this->Session->setFlash(__('Comment was not deleted'));
    }
    $this->redirect(array('controller' => 'posts', 'action' => 'view', $id));        
}

}

Plenix,这不是完全错误的吗?您正在删除评论并将评论的 id 传递给帖子的视图控制器?那么,您是在查看帖子或评论吗?还有什么建议吗?

于 2012-04-30T12:15:42.697 回答