对本地方法使用 EXPECT_CALL

Using EXPECT_CALL for local methods

我知道 EXPECT_CALL 应该用于模拟 类 和他们的 objects/methods。但是有没有可能使用它来期望调用本地方法?

void Sample::Init()
{
   // some codes here...

   auto enabled = isFeatureEnabled();

   //some other things here
}

bool Sample::isFeatureEnabled()
{
   return lights_ and sounds_;
}

我想 EXPECT_CALL isFeatureEnabled() - 这可能吗?

你可以试试这个,我觉得这个方法很有用:

class template_method_base {
public:
  void execute(std::string s1, std::string s2) {
    delegate(s1 + s2);
  }

private:
  virtual void delegate(std::string s) = 0;
};

class template_method_testable : public template_method_base {
public:
  MOCK_METHOD1(delegate, void(std::string s));
};

TEST(TestingTemplateMethod, shouldDelegateCallFromExecute) {
  template_method_testable testable_obj{};

  EXPECT_CALL(testable_obj, delegate("AB"));

  testable_obj.execute("A", "B");
}