使用 Mockery::contains( ) 检查 Mockery 参数
Checking Mockery arguments using Mockery::contains( )
我正在使用 Mockery 测试创建日历事件的 class。它将开始日期和结束日期的时间戳传递给我的 EventRepository 的 create()
方法。还有其他数据作为参数包含在内,但我只关心此测试的时间戳是否正确。
此代码可以很好地测试 one 事件的创建:
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466460000, 1466467200));
然而,当我将 Mock 扩展到被调用 两次 的 create()
方法时,它失败了。
$this->repo->shouldReceive('create')->twice()
->with(Mockery::contains(1466460000, 1466467200),
Mockery::contains(1466632800, 1466640000));
Mockery 不使用语法 ->with(args1, args2)
来指定多次调用同一函数的参数吗?
当被测方法有多个参数时,使用多参数语法。
对于您的用例,您需要执行以下操作:
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466460000, 1466467200));
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466632800, 1466640000));
我正在使用 Mockery 测试创建日历事件的 class。它将开始日期和结束日期的时间戳传递给我的 EventRepository 的 create()
方法。还有其他数据作为参数包含在内,但我只关心此测试的时间戳是否正确。
此代码可以很好地测试 one 事件的创建:
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466460000, 1466467200));
然而,当我将 Mock 扩展到被调用 两次 的 create()
方法时,它失败了。
$this->repo->shouldReceive('create')->twice()
->with(Mockery::contains(1466460000, 1466467200),
Mockery::contains(1466632800, 1466640000));
Mockery 不使用语法 ->with(args1, args2)
来指定多次调用同一函数的参数吗?
当被测方法有多个参数时,使用多参数语法。
对于您的用例,您需要执行以下操作:
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466460000, 1466467200));
$this->repo->shouldReceive('create')->once()
->with(Mockery::contains(1466632800, 1466640000));