【问题标题】:ZF2 phpUnit test view helperZF2 phpUnit 测试视图助手
【发布时间】:2015-04-22 09:10:09
【问题描述】:

不幸的是,我在 ZF2 和单元测试中绝对为零。 我写了简单的视图助手(Zend Translate 的包装函数),现在我必须为此编写一个测试。我正在尝试在现有测试类中添加一个测试,所以一切都已配置......也许。我只需要开始使用这段代码:

public function testTranslation()
{
    $result = $this->t('about');
    $expected = "About";
    $this->assertEquals($exp, $res);
}

我也尝试像在控制器中那样获得功能,但没有运气:

$t = $this->getServiceLocator()->get('ViewHelperManager')->get('t');
$result = $t('about');

有什么帮助吗?

【问题讨论】:

    标签: unit-testing zend-framework2 phpunit


    【解决方案1】:

    您可以简单地创建视图助手的实例。单元测试的目的是只测试单个单元(在这种情况下是您的视图助手)。

    当您从 serviceManager 检索帮助程序时,您需要引导一个完整的 ZF2 应用程序。现在您不仅要测试助手,还要测试所有涉及的组件。这称为集成测试。

    您的单元测试看起来像这样:

    public function testTranslation()
    {
        $helper = new TranslateHelper();
        $result = $helper->__invoke('about');
        $expected = "About";
        $this->assertEquals($exp, $res);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多