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

发布于 2024-07-17 08:32:42 字数 826 浏览 10 评论 0原文

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

How do I get (或至少测试)控制器已设置有效模型?

In Zend, models are added to the view:

//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):

//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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-07-24 08:32:42

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

由于这可能不是最佳方法,我仍然很想听听其他有潜在解决方案的人的意见。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文