21

我有一个看起来像这样的模拟对象设置:

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

有没有办法不用重复.WillOnce(Return(1))三遍?

4

3 回答 3

38
using testing::InSequence;

MyObject obj;

{
  InSequence s;
  EXPECT_CALL(obj, myFunction(_))
      .Times(3)
      .WillRepeatedly(Return(1));
  EXPECT_CALL(obj, myFunction(_))
      .WillRepeatedly(Return(-1));
}
于 2013-08-08T16:24:09.470 回答
4

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

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

如果你知道你希望你的最后/默认响应是什么,这可能会很有用(-1),但又想在此之前调用它的次数。

于 2015-10-30T19:12:15.210 回答
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.

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

于 2013-08-07T21:18:58.393 回答