我正在使用Mockery来测试创建日历事件的类。 它将开始和结束日期的时间戳传递给我的EventRepository的create()方法。 还有其他数据作为参数包含在内,但我只关心时间戳对于此测试是否正确。
此代码适用于测试一个事件的创建:
$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)来指定多个调用同一函数的参数吗?
I am using Mockery to test a class that creates calendar events. It passes timestamps of the start and end date to my EventRepository's create() method. There is other data included as arguments, but I only care that the timestamps are correct for this test.
This code works fine for testing the creation of one event:
$this->repo->shouldReceive('create')->once() ->with(Mockery::contains(1466460000, 1466467200));However when I extend the Mock to the create() method being called twice, it fails.
$this->repo->shouldReceive('create')->twice() ->with(Mockery::contains(1466460000, 1466467200), Mockery::contains(1466632800, 1466640000));Doesn't Mockery use the syntax ->with(args1, args2) for specifying the arguments for multiple calls to the same function?
最满意答案
当被测方法有多个参数时,使用带有多个参数的语法。
对于您的用例,您需要执行以下操作:
$this->repo->shouldReceive('create')->once() ->with(Mockery::contains(1466460000, 1466467200)); $this->repo->shouldReceive('create')->once() ->with(Mockery::contains(1466632800, 1466640000));The syntax with the multiple arguments are used when the method under test has multiple arguments.
For your use case you'll need to do as follows:
$this->repo->shouldReceive('create')->once() ->with(Mockery::contains(1466460000, 1466467200)); $this->repo->shouldReceive('create')->once() ->with(Mockery::contains(1466632800, 1466640000));更多推荐
发布评论