【问题标题】:Testing __call method测试 __call 方法
【发布时间】:2013-07-19 14:10:45
【问题描述】:
public function __call($method, $args)
{
    if ( ! in_array($method, $this->validMethods))
    {
      throw new \BadMethodCallException("Not a valid method: {$method}");
    }

}

如何测试__call 方法以确保$method 在我的有效方法列表中?现在这就是我所做的;

/**
 * @covers World\Transmission::__call()
 * @expectedException BadMethodCallException
 * @expectedExceptionMessage Not a valid method: foo
 */
public function test__callInvalidRequest()
{
    $m = m::mock('World\\Transmission', array($this->config))->makePartial();
    $m->foo(array('foo'));
}

我得到的错误是call_user_func_array()的无尽痕迹。

Maximum function nesting level of '100' reached, aborting!.
...

【问题讨论】:

  • 你班上有什么:World\Transmission
  • 如果删除$m->foo(array('foo'));,仍然达到100嵌套级别?
  • 现在会抛出Failed asserting that exception of type "BadMethodCallException" is thrown.
  • 你使用phpunit测试框架吗?

标签: php phpunit mockery


【解决方案1】:

如何简单地将您的测试代码更改为如下:

    public function test__callInvalidRequest()
    {
        $transmission = new World\Transmission($this->config);
        $transmission->foo();
    }

【讨论】:

  • 哈哈,伙计,我不知道我为什么要强迫自己使用模拟。
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多