【问题标题】:Test exception in CodeIgniter using PHPUnit/CIUnit使用 PHPUnit/CIUnit 在 CodeIgniter 中测试异常
【发布时间】:2014-08-18 05:32:55
【问题描述】:

我在 person_model.php 中有一个方法:

public function getInfo()
{
    $result = array();

    try
    {
        // do something
    }
    catch(Exception $exception)
    {
        log_message('error', $exception->getMessage());
    }
    finally
    {
        return $result;
    }

}

如何测试异常并确保在捕获异常时调用 log_message() 方法?

谢谢!!!

【问题讨论】:

    标签: php codeigniter exception phpunit


    【解决方案1】:

    这不是很容易做到的,主要是因为没有办法模拟对 log_message 的调用。但是,从上面的代码中,您可以测试返回值是什么,或者如果抛出异常,可能会更改为什么。例如,假设如果抛出异常,我们可以确定只有在这种情况下,返回的值才会是一个空数组。对于这个例子,我们只是测试返回的计数为 0。

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2011-03-29
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多