我在测试时遇到同样的问题
public function testReturnUser(){
$faker = Factory::create();
$user = new User($faker->userName, $faker->email);
$mockFinder = \Mockery::mock(ClassFinder::class);
$mockFinder->shouldReceive('find')
->with(User::class, 1)
->andReturn($user);
$helper = new Helper($mockFinder);
$this->assertEquals([$user], $helper->getMethod(User::class));
}
并在Helper类中
class Helper {
private $finder;
public function __constructor(ClassFinder $finder){
$this->finder = $finder;
}
public function getMethod(string $className, array $data =[]){
return $this->finder->find($className, $data['id']);
}
}
所以问题是,当我在没有第二个可选参数的情况下调用getMethod
并运行测试时,我发现$data['id']
是无效索引,
虽然我将参数传递给了模拟对象。那么,如果有人知道为什么会这样?