避免在 Google Mock 中多次匹配 .WillOnce

问题描述 投票:0回答:3

我有一个模拟对象设置,如下所示:

MyObject obj;
EXPECT_CALL(obj, myFunction(_))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillRepeatedly(Return(-1));

有没有办法不用重复

.WillOnce(Return(1))
三遍?

c++ tdd googlemock
3个回答
47
投票

您可以使用
.Times(nn)
后接
.WillRepeatedly(...
来解决此问题:

using testing::InSequence;

MyObject obj;

{
  InSequence s;
  EXPECT_CALL(obj, myFunction(_))
      .Times(3)
      .WillRepeatedly(Return(1));
  EXPECT_CALL(obj, myFunction(_))
      .WillRepeatedly(Return(-1));
}

4
投票

为了完整起见,还有另一个标准/简单的选项,尽管在这种情况下接受的答案似乎更清晰。

EXPECT_CALL(obj, myFunction(_)).WillRepeatedly(Return(-1));
EXPECT_CALL(obj, myFunction(_)).Times(3).WillRepeatedly(Return(1)).RetiresOnSaturation();

如果您知道您希望最后/默认响应是什么(

-1
),但又想搞乱之前调用的次数,这会很有用。


1
投票

恐怕没有其他方法可以配置此行为。至少在文档中找不到明显的方法。

您可能会通过引入适当的用户定义的匹配器来解决这个问题,它可以跟踪您可以通过模板参数从测试用例中提供的调用计数和阈值(实际上不知道如何诱导

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
                    >()
               );
}

使用 then 来期望一个确定计数的调用结果:

EXPECT_CALL(blah,method)
   .WillRepeatedly(MyCountingReturnMatcher<1000,int,1,-1>()) // Checks that method
                                                             // returns 1000 times 1
                                                             // but -1 on subsequent 
                                                             // calls.

注意
我没有检查这段代码是否按预期工作,但它应该引导您走向正确的方向。

© www.soinside.com 2019 - 2024. All rights reserved.