使用Mockery :: contains()检查Mockery参数(Checking Mockery arguments using Mockery::contains( ))

编程入门 行业动态 更新时间:2024-10-28 20:30:26
使用Mockery :: contains()检查Mockery参数(Checking Mockery arguments using Mockery::contains( ))

我正在使用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));

更多推荐

本文发布于:2023-07-27 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1294022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   Mockery   arguments   Checking

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!