【问题标题】:How to test an action's output in ZF2/ZF3?如何在 ZF2/ZF3 中测试动作的输出?
【发布时间】:2017-07-31 10:54:12
【问题描述】:

如何在 ZF2/ZF3 中捕捉控制器动作的 (ViewModel) 输出?

背景:

我正在为 Zend Framework 3 应用程序(刚从 ZF2 迁移)编写一些集成测试。我正在使用 PHPUnit v6.2.2 和 Zend\Test v3.1.0。我想测试从调用路由的那一刻到保存/检索数据的那一刻的过程部分。这意味着测试所有控制器操作的方向:

  1. 数据按预期保存(为此我想调用路由/操作,然后检查新的数据库状态);
  2. 按预期检索数据(为此,我想调用路由/操作,然后检查操作的输出)。

第一个方向很明确:调用路由后,我只需启动普通的数据库请求并检查是否存在预期的变化。

public function testBuzAction()
{
    $this->dispatch('/foo/bar/buz');
    // Here might be optionally some asserts, whether the correct action is called...
    // Here are the database checks...
}

但对于另一个方向,我们需要操作返回的ViewModel。

public function testBuzAction()
{
    $this->dispatch('/foo/bar/buz');
    // Here might be optionally some asserts, whether the correct action is called...
    // Here is the ViewModel output of the Bar#buzAction() analyzed.
}

如何在 PHPUnit 测试中获取操作的输出?

【问题讨论】:

    标签: unit-testing zend-framework2 integration-testing zend-framework3 controller-action


    【解决方案1】:
    public function testBuzAction()
    {
        $this->dispatch('/foo/bar/buz');
        ...
        $viewModelReturnedByAction = $this->getApplication()->getMvcEvent()->getResult();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2012-09-28
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      相关资源
      最近更新 更多