Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当测函数有多个参数时还可以指定出参吗? #30

Open
hahhhhh opened this issue Aug 19, 2022 · 11 comments
Open

当测函数有多个参数时还可以指定出参吗? #30

hahhhhh opened this issue Aug 19, 2022 · 11 comments

Comments

@hahhhhh
Copy link

hahhhhh commented Aug 19, 2022

比如测试函数 void function(int in1, int2, int &out1, int &out2),可以分别指定出参out1 out2为某个值吗

@orca-zhang
Copy link
Contributor

引用传值可以用在.with里面加outBound(obj, <输入参数约束>)把传入的obj变量来输出
这里的输入参数约束就像any()eq(1)之类

或者也可以用.invoke(mocker),设置指定的mock函数来完成需要的功能

@hahhhhh
Copy link
Author

hahhhhh commented Aug 22, 2022

就是比如只想指定out2的出参,out1不管,那么.with(outBound(var, <输入参数约束>)) 这里的输入参数约束该怎么指定为out2呢

@orca-zhang
Copy link
Contributor

.with(any(), any(), any(), outBound(var, <输入参数约束>))

这里的out2对应var

@hahhhhh
Copy link
Author

hahhhhh commented Aug 22, 2022

试了下没问题,但是我发现如果在多个test宏中mock了同一个函数,每次返回不同值,但实际这个函数每次的返回值都只会按第一次mock时指定的参数来。也就是说不同的test宏所mock的函数会相互影响,这个有办法避免么?只让mock的函数在当前的test宏里生效

@orca-zhang
Copy link
Contributor

没有看太明白,每一个EMOCK对应都是一条单独的mock规范,如果想要有多种返回,可以设置多条不同规则的规范,比如在参数满足某些条件的时候才生效


只让mock的函数在当前的test宏里生效

这个本来应该有一个caller约束的,能够约定调用者是某个函数的时候才能生效,不过目前是作废状态,还待修复

@hahhhhh
Copy link
Author

hahhhhh commented Aug 25, 2022

再问一下,我看到以前也有人问到常函数的mock,现在可以直接mock了吗,我试了以前说的用全局函数的方式并不行

@orca-zhang
Copy link
Contributor

orca-zhang commented Aug 26, 2022

b7f60ef 14707f4 这两个提交增加了常成员函数的支持,感谢反馈

可以参考例子:

EMOCK(&CUT::normal_method_2)

@hahhhhh
Copy link
Author

hahhhhh commented Aug 27, 2022

.with(eq(const_cast<CUT const*>(&cut)), eq(2))
例子的代码中这里如果无法拿到cut,能否配合spy拿到常函数被调用时cut的地址

@hahhhhh
Copy link
Author

hahhhhh commented Aug 27, 2022

image
举个例子,我需要对class A的normal_method_1函数做测试,但是函数里却调用了class B的常函数,这种常函数的mock可以实现吗

@orca-zhang
Copy link
Contributor

orca-zhang commented Aug 28, 2022

可以呀,.with这个可以不写的,也就是全局生效,不匹配指定的对象

EMOCK(&CUT::normal_method_2)
      .stubs()
      .will(invoke(normal_method_2));

像例子的eq(const_cast<CUT const*>(&cut))条件也可以写成any()来实现匹配任意的CUT const对象

EMOCK(&CUT::normal_method_2)
      .stubs()
      .with(any(), eq(2))
      .will(invoke(normal_method_2));

这种情况下,主要是为了说明后面第二个参数的eq(2),所以第一个参数要有一个占位,否则可以直接不写.with

@hahhhhh
Copy link
Author

hahhhhh commented Aug 29, 2022

好的,感谢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants