c – 如何仅覆盖一个EXPECT_CALL的默认ON_CALL操作,并稍后返回默认操作

我想测试我的系统的方法,其返回值部分取决于对某种连接接口的调用的返回值.在大多数情况下,我希望IConnection在对它的open(_,_)方法进行任何调用时返回true.除了一种情况,当我明确测试连接失败的条件时.

例:

/*
 * Some kind of network interface with method `open`
 */
class IConnection {
public:
    IConnection() = default;
    virtual ~IConnection() = default;
    virtual bool open(const std::string& address, int port) = 0;
};

class ConnectionMock: public IConnection {
public:
    MOCK_METHOD2(open, bool(const std::string& address, int port));
};

class MySystem {
public:
    MySystem() = delete;
    MySystem(std::shared_ptr<IConnection> connection): connection_(connection) {}
    bool doSth() {
        /*
         * Do some things, but fail if connection fails
         */
        bool connectionStatus = connection_->open("127.0.0.1", 6969);
        if (!connectionStatus) {
            return false;
        }
        // do other things
        return true;
    }
private:
    std::shared_ptr<IConnection> connection_;
};

TEST(MySystemShould, returnFalseIfFailedToOpenConnectionAndTrueIfSucceeded) {
    auto connectionMock = std::make_shared<NiceMock<ConnectionMock> >();
    ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));
    MySystem system(connectionMock);
    // if I don't specify Times test fill fail, because WillOnce automatically sets Times(1)
    EXPECT_CALL(*connectionMock, open(_, _)).Times(AnyNumber()).WillOnce(Return(false));
    /*
     * Commented code below is not a good solution - after expectation retires
     * the test will fail upon subsequent calls
     */
    //EXPECT_CALL(*connectionMock, open(_, _)).WillOnce(Return(false)).RetiresOnSaturation();
    ASSERT_FALSE(system.doSth());
    /*
     * Code bellow allows me to avoid the warning
     */
    //EXPECT_CALL(*connectionMock, open(_, _)).WillRepeatedly(Return(true));
    ASSERT_TRUE(system.doSth());
}

我当前解决方案的问题是,当EXPECT_CALL覆盖变得饱和时,即使gmock返回到ON_CALL上指定的默认操作,每次后续打开(_,_)的调用都会导致以下警告:

GMOCK WARNING:
/whatever.cpp:105: Actions ran out in EXPECT_CALL(*connectionMock, open(_, _))...
Called 2 times, but only 1 WillOnce() is specified - taking default action specified at:
/whatever.cpp:103:

即使我正在使用NiceMock.我可以通过使用WillRepeated(返回(true))指定EXPECT_CALL来消除警告,但这是我在ON_CALL中的代码重复.

我想知道,我怎样才能覆盖用ON_CALL指定的默认操作,只需调用一次IConnection :: open,然后返回默认值,而不会导致gmock打印警告.完美的解决方案类似于:

EXPECT_CALL(*connectionMock, open(_, _)).WillOnce(Return(false)).DisableExpectationAfterSaturation();

但它不存在. RetiresOnSaturation不能正常工作,因为它在饱和后未通过测试(与ON_CALL指定的操作不匹配).

最佳答案
编辑2
DoDefault() – 功能接近问题中提出的问题.它指定EXPECT_CALL中的操作应该返回到ON_CALL指定的默认操作:

using ::testing::DoDefault;

// Default action
ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));

// returns true once and then goes back to the default action
EXPECT_CALL(*connectionMock, open(_, _)
  .WillOnce(Return(false))
  .WillRepeatedly(DoDefault());

初步答复

如果IConnection :: open的返回值取决于参数,则可以指定ON_CALL两次但使用不同的参数(或者更确切地说是参数而不是占位符):

ON_CALL(*connectionMock, open(_, _)).WillByDefault(Return(true));
ON_CALL(*connectionMock, open("BAD_ADDRESS", 20)).WillByDefault(Return(false));

因此,只要使用参数“BAD_ADDRESS”和20调用模拟方法open,它将返回false,否则返回true.

这是一个简单的例子:

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;

class A {
 public:
  virtual bool bla(int a) = 0;
};

class MOCKA : public A {
 public:
  MOCK_METHOD1(bla, bool(int));
};

TEST(ABC, aBABA) {
  MOCKA a;
  ON_CALL(a, bla(_)).WillByDefault(Return(false));
  ON_CALL(a, bla(1)).WillByDefault(Return(true));

  EXPECT_CALL(a, bla(_)).Times(AnyNumber());

  EXPECT_TRUE(a.bla(1));
  EXPECT_TRUE(a.bla(1));
  EXPECT_TRUE(a.bla(1));
  EXPECT_FALSE(a.bla(2));
  EXPECT_FALSE(a.bla(3));
  EXPECT_FALSE(a.bla(4));
}

编辑1
我想现在我理解了这个问题,如果我做了,那么解决方案非常简单:

EXPECT_CALL(*connectionMock, open(_, _))
      .Times(AnyNumber())
      .WillOnce(Return(true))
      .WillRepeatedly(Return(false));

当在MySystem :: doSth中调用ConnectionMock :: open时,它将返回true,然后无论参数是什么,总是返回false.在这种情况下,您也不需要指定ON_CALL.或者您肯定需要使用ON_CALL而不是EXPECT_CALL指定操作?

转载注明原文:c – 如何仅覆盖一个EXPECT_CALL的默认ON_CALL操作,并稍后返回默认操作 - 代码日志