gpt4 book ai didi

php - Symfony 在我的单元测试中模拟一个特定的方法

转载 作者:行者123 更新时间:2023-11-28 20:43:53 25 4
gpt4 key购买 nike

我在尝试模拟服务以便对其进行单元测试时遇到问题。

在我的测试课上,

    $mock = $this->getMock('MyClass');
$mock->expects($this->any())->method('method_2')->will($this->returnValue('fake_value'));

在我的服务中,method_2() 调用另一个方法(比方说 method_1()')

因为我只想对 method_1() 进行单元测试,所以我只需要模拟 method_2()但是当我运行这个测试时,method_1() 已经返回 null。

你知道为什么我已经得到 null 了吗?

最佳答案

看看getMock() helper signature ,它允许您将一组方法作为第二个参数传递,然后模拟这些方法并返回 null(除非您定义了每个方法应返回的值)

在您的情况下,所有方法都被模拟并返回 null,但您强制返回值的 method_2() 除外。

重试并替换,

$mock = $this->getMock('MyClass');

通过,

$mock = $this->getMock('MyClass', array('method_2'));

关于php - Symfony 在我的单元测试中模拟一个特定的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com