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

时间:2021-06-18 07:32:31

In Zend, models are added to the view:

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

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

We can easily check that "model" exists in the view (I'm using simpletest for this):

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

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

However, testing the "value" doesn't work as well:

但是,测试“价值”也不起作用:

//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'));
  }

How do I get (or at least test) that the controller has set a valid model?

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

2 个解决方案

#2


So, the solution (at least the planned one at this time) is make a testable view that implements Zend_View_Interface. This will include a "get" method that returns objects passed to "__set". Then we'll hook up the controller to use this view during the test bootstrapping process.

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

Since this may not be the optimal approach, I'd still love to hear from others who have potential solutions.

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

#1


#2


So, the solution (at least the planned one at this time) is make a testable view that implements Zend_View_Interface. This will include a "get" method that returns objects passed to "__set". Then we'll hook up the controller to use this view during the test bootstrapping process.

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

Since this may not be the optimal approach, I'd still love to hear from others who have potential solutions.

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