【问题标题】:phpspec - thows during but use if not?phpspec - 期间但如果没有使用?
【发布时间】:2013-11-09 02:47:46
【问题描述】:

在 phpspec 我可以测试这样的东西:

function it_must_be_constructed_with_my_variable()
{
    $this->shouldThrow(new \Exception('bla'))->during('__construct', array('variable' => 'value'));
}

但是如果我想确保在没有传递给函数的情况下引发异常怎么办?

即我想说如果传递的数组不等于某个值,就会抛出异常。

【问题讨论】:

  • 构造函数是规范测试的有效目标吗?但是尽管如此,您的问题可以改写:如果未传递参数,如何测试是否发生了某些事情?我的第一个猜测是要么传递一个空参数数组,要么完全忽略它。

标签: phpunit phpspec


【解决方案1】:

如果我理解正确,您想测试当用户传递一个数组时是否抛出异常,而该实现不知道或无法处理。

那么你可以举个例子,将“错误”数组作为参数传递

【讨论】:

    【解决方案2】:

    我相信您可以简单地执行以下操作:

    function it_should_throw_exception_if_constructed_with_wrong_variable()
    {
        $myWrongVariable = array('something' => 'wrong');
        $this->shouldThrow(new \Exception('bla'))->during('__construct', array($myWrongVariable));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多