C++ 避免在 Google Mock 中多次匹配 .WillOnce
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18112454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid matching .WillOnce multiple times in Google Mock
提问by UXkQEZ7
I have a mock object setup that looks like this:
我有一个模拟对象设置,如下所示:
MyObject obj;
EXPECT_CALL(obj, myFunction(_))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillRepeatedly(Return(-1));
Is there a way to not have to repeat .WillOnce(Return(1))
three times?
有没有办法不用重复.WillOnce(Return(1))
三遍?
回答by VladLosev
using testing::InSequence;
MyObject obj;
{
InSequence s;
EXPECT_CALL(obj, myFunction(_))
.Times(3)
.WillRepeatedly(Return(1));
EXPECT_CALL(obj, myFunction(_))
.WillRepeatedly(Return(-1));
}
回答by dwanderson
For completeness' sake, there's another standard/simple option, though the accepted answer seems clearer in this case.
为了完整起见,还有另一个标准/简单选项,尽管在这种情况下接受的答案似乎更清楚。
EXPECT_CALL(obj, myFunction(_)).WillRepeatedly(Return(-1));
EXPECT_CALL(obj, myFunction(_)).Times(3).WillRepeatedly(Return(1)).RetiresOnSaturation();
This can be useful if you know what you want your last/default response to be (-1
), but want to muck with the number of times its called before then.
如果您知道您希望上次/默认响应是什么 ( -1
),但想要在此之前调用它的次数,这会很有用。
回答by π?ντα ?ε?
I'm afraid there's no other way to configure this behavior. Can't find an obvious way in the documentation at least.
恐怕没有其他方法可以配置此行为。至少在文档中找不到明显的方法。
You'll might get off this problem introducing an appropriate user defined matcherthough, that keeps track of a calling count and threshold you can provide from your testcases via template parameters (don't actually know how to induce the ResultType
automatically :-( ):
不过,您可能会通过引入一个适当的用户定义匹配器来解决这个问题,它会跟踪您可以通过模板参数从测试用例中提供的调用计数和阈值(实际上并不知道如何ResultType
自动诱导:-( ):
using ::testing::MakeMatcher;
using ::testing::Matcher;
using ::testing::MatcherInterface;
using ::testing::MatchResultListener;
template
< unsigned int CallThreshold
, typename ResultType
, ResultType LowerRetValue
, ResultType HigherRetValue
>
class MyCountingReturnMatcher
: public MatcherInterface<ResultType>
{
public:
MyCountingReturnMatcher()
: callCount(0)
{
}
virtual bool MatchAndExplain
( ResultType n
, MatchResultListener* listener
) const
{
++callCount;
if(callCount <= CallThreshold)
{
return n == LowerRetValue;
}
return n == HigherRetValue;
}
virtual void DescribeTo(::std::ostream* os) const
{
if(callCount <= CallThreshold)
{
*os << "returned " << LowerRetValue;
}
else
{
*os << "returned " << HigherRetValue;
}
}
virtual void DescribeNegationTo(::std::ostream* os) const
{
*os << " didn't return expected value ";
if(callCount <= CallThreshold)
{
*os << "didn't return expected " << LowerRetValue
<< " at call #" << callCount;
}
else
{
*os << "didn't return expected " << HigherRetValue
<< " at call #" << callCount;
}
}
private:
unsigned int callCount;
};
template
< unsigned int CallThreshold
, typename ResultType
, ResultType LowerRetValue
, ResultType HigherRetValue
>
inline Matcher<ResultType> MyCountingReturnMatcher()
{
return MakeMatcher
( new MyCountingReturnMatcher
< ResultType
, CallThreshold
, ResultType
, LowerRetValue
, HigherRetValue
>()
);
}
Use then to expect a certainly counted call result:
使用 then 来期待一个肯定计数的调用结果:
EXPECT_CALL(blah,method)
.WillRepeatedly(MyCountingReturnMatcher<1000,int,1,-1>()) // Checks that method
// returns 1000 times 1
// but -1 on subsequent
// calls.
NOTE
I didn't check this code to be working as intended, but it should guide you into the right direction.
注意
我没有检查此代码是否按预期工作,但它应该引导您进入正确的方向。