php Mockery 错误 "call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Ex...

错误写法

$mock = Mockery::mock(MyClass::class)
    ->shouldReceive('foo')
    ->once()
    ->with($arg)
    ->andReturn($returnValue);

 

Mockery::mock(MyClass::class) 返回的是 \Mockery\MockInterface

 

而后面的几个方法都是 \Mockery\Expectation 里面的方法。

 

最后我们调用 mock 实例的方法时需要的是 \Mockery\MockInterface,而不是  \Mockery\Expectation , 所以正确的写法如下:

$mock = Mockery::mock(MyClass::class);
$mock->shouldReceive('foo')
    ->once()
    ->with($arg)
    ->andReturn($returnValue);
var_dump($mock->foo(3) === 5);

 

又或者在第一种写法后面 $mock->getMock()->foo(3) 这样获取 mock 实例。

 

转载于:https://www.cnblogs.com/eleven24/p/10430426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 针对这个需求规格说明,可以设计以下测试用例: - 客户为批发型企业,订货数大于 50,发货距离不超过 50 公里,应该有 15% 折扣 - 客户为批发型企业,订货数大于 50,发货距离超过 50 公里,应该有 10% 折扣 - 客户为非批发型企业,订货数大于 100,发货距离不超过 50 公里,应该有 10% 折扣 - 客户为非批发型企业,订货数大于 100,发货距离超过 50 公里,应该有 5% 折扣 - 客户为批发型企业,订货数不足 50,无折扣 - 客户为非批发型企业,订货数不足 100,无折扣 - 客户为批发型企业,发货距离超过 50 公里,无折扣 - 客户为非批发型企业,发货距离超过 50 公里,无折扣 2. 使用 JUnit 测试框架和 JMock 工具,可以编写以下测试代码: ```java import org.junit.Assert; import org.junit.Test; import org.jmock.Mockery; import org.jmock.Expectations; public class DiscountCalculatorTest { private Mockery context = new Mockery(); @Test public void testCalculateDiscount() { final FreightCalculator mockFreightCalculator = context.mock(FreightCalculator.class); // 设置桩模块,模拟计算运费的行为 context.checking(new Expectations() {{ allowing(mockFreightCalculator).calculateFreight(with(any(Order.class)))); will(returnValue(40.0)); }}); // 客户为批发型企业,订货数大于 50,发货距离不超过 50 公里,应该有 15% 折扣 double discount = DiscountCalculator.calculateDiscount(true, 60, mockFreightCalculator); Assert.assertEquals(0.15, discount, 0.0001); // 客户为批发型企业,订货数大于 50,发货距离超过 50 公里,应该有 10% 折扣 discount = DiscountCalculator.calculateDiscount(true, 60, mockFreightCalculator); Assert.assertEquals(0.1, discount, 0.0001); // 客户为非批发型企业,订货数大于 100,发货距离不超过 50 公里,应该有 10% 折扣 discount = DiscountCalculator.calculateDiscount(false, 120, mockFreightCalculator); Assert.assertEquals(0.1, discount, 0.0001); // 客户为非批发型企业,订货数大于 100,发货距离超过 50 公里,应该有 5% 折扣 discount = DiscountCalculator.calculateDiscount(false, 120, mockFreightCalculator); Assert.assertEquals(0.05, discount, 0.0001); // 客户为批发型企业,订货数不足 50,无折扣 discount = DiscountCalculator.calculateDiscount(true, 30, mockFreightCalculator); Assert.assertEquals(0, discount, 0.0001); // 客户为非批发型企业,订货数不足 100,无折扣 discount = DiscountCalculator.calculateDiscount(false, 80, mockFreightCalculator); Assert.assertEquals(0, discount, 0.0001); // 客户为批发型企业,发货距离超过 50 公里,无折扣 discount = DiscountCalculator.calculateDiscount(true, 60, mockFreightCalculator); Assert.assertEquals(0, discount, 0.0001); // 客户为非批发型企业,发货距离超过 50 公里,无折扣 discount = DiscountCalculator.calculateDiscount(false, 120, mockFreightCalculator); Assert.assertEquals(0, discount, 0.0001); // 验证桩模块的调用次数 context.assertIsSatisfied(); } } ``` 在测试代码中,我们使用 JMock 工具来模拟 FreightCalculator 类,并设置桩模块,模拟计算运费的行为。同时,我们针对不同的输入,通过 `assertEquals` 方法来验证输出结果是否符合预期。如果任意一个测试用例失败,那么测试框架会自动提示测试失败。通过编写这样的单元测试代码,开发者可以确保自己的业务逻辑代码在各种情况下都能够正确运行,并且与其他模块的协作也能够正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值