单元测试Zend控制器 - 如何测试视图中设置的内容

时间:2009-04-16 14:50:18

标签: php unit-testing zend-framework simpletest

在Zend中,模型被添加到视图中:

//In a controller
public function indexAction() {
  //Do some work and get a model
  $this->view->model = $model;    
}

我们可以轻松检查视图中是否存在“模型”(我正在使用simpletest):

//In a unit test
  public function testModelIsSetInView() {
    //Call the controllers index action
    $this->assertTrue(isset($this->controller->view->model));
  }

然而,测试“价值”也不起作用:

//In a unit test
  public function testModelValue() {
    //Call the controllers index action

    //Both of these return null, though I'd like to access them!
    $this->assertNull($this->controller->view->model);
    $this->assertNull($this->controller->view->__get('model'));
  }

如何获得(或至少测试)控制器已设置有效模型?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

因此,解决方案(至少是此时计划的解决方案)是一个实现Zend_View_Interface的可测试视图。这将包括一个“get”方法,它返回传递给“__set”的对象。然后我们将连接控制器以在测试引导过程中使用此视图。

由于这可能不是最佳方法,我仍然希望听到其他有潜在解决方案的人。