首页 > 解决方案 > 我可以忽略模拟方法中的参数类型检查吗?

问题描述

我正在为依赖于另一个服务类的服务类编写测试。

Foo.php

class Foo {

 public function __construct(Bar $bar)
 {
  $this->bar = $bar;
 }

 public function fooMethod()
 {
  $getSomeArrayOrNull = null;
  $this->bar->barMethod($getSomeArrayOrNull);
 }

}

酒吧.php

class Bar {


 public function barMethod(array $array)
 {
   return null;
 }

}

测试.php

class Test extends TestCase {
 public function testIt()
 {
  $bar = Mockery::mock(Bar::class);
  $bar->shouldReceive('barMethod')->withAnyArgs()->andReturn(true);
  $foo = new Foo();
  // this throws an error because invalid argument type in Bar method
  $foo->fooMethod();
 }
}

我可以以某种方式告诉嘲弄 ingore 类型检查$bar->barMethod()吗?我正在编写单元测试,我想完全模拟这部分代码。

这是一种不好的代码气味,我是否$getSomeArrayOrNull也应该模拟该属性?(女巫意味着总是一个数组)

标签: phpunit-testingmockery

解决方案


推荐阅读