C++ Gmock - 使用 shared_ptr <FactoryClass> 的测试函数

标签 c++ factory factory-pattern googlemock

我是 gtest/gmock 的新手,正在尝试用 c++ 测试一个简单的函数,它有两个指针“m_propBsh_p”和“m_eBsh_p”;这些指针在一些工厂创建后变得有效,但是我不想参与工厂类的复杂性和回调。 以下是我要为其编写测试的函数定义:

    std::string Foo::toString(const std::string &indent) const
    {
    ....

    std::string str =
      (m_propBsh_p != nullptr) ? m_propBsh_p -> toString("P-BSH: ") : "-";
    str +=
      (m_eBsh_p != nullptr) ? m_eBsh_p -> toString("E-BSH: ") : "-";

    return str;
  }

由于我只对测试这个特定的 toString 函数感兴趣,因此我只想为“m_propBsh_p”和“m_eBsh_p”提供有效的指针。我正在瞄准/尝试以下内容:

  //Assuming to have mocked class for pointers
  std::shared_ptr<MockedBshClass> m_mockEBsh_p;
  std::shared_ptr<MockedBshClass> m_mockPropBsh_p;

  TEST_F(FooTest, toStringBshInfoPass)
  {
    std::string eBshAndpBshStr = "eBshAndpBshStr";

    ON_CALL(*m_mockPropBsh_p, toString(_)).WillByDefault(Return(eBshAndpBshStr));
    ON_CALL(*m_mockEBsh_p, toString(_)).WillByDefault(Return(eBshAndpBshStr ));
    //EXPECT_CALL((*m_mockPropBsh_p), toString(_)).Times(1);
    //EXPECT_CALL((*m_mockEBsh_p), toString(_)).Times(1);

    //Call mock or some fake function which makes m_propBsh_p & m_eBsh_p valid.
    foo->makePtrValidAgain(); //however this is a complex function which bring more callbacks and complexity and i do not want to call, instead i want to have some fake/mocked function which just gives me valid pointers

    EXPECT_THAT(foo->toString(""),HasSubstr(eBshAndpBshStr+eBshAndpBshStr));
  }

以下是 Foo 类和指针的位背景:

   Class Foo : fooParent..
   {
   ...
    void makePtrValidAgain();
    std::string toString();
    ..
    typedef std::shared_ptr<BshClass> m_propBsh_p;
    typedef std::shared_ptr<BshClass> m_eBsh_p; 
   ...
   }; 

   void Foo::makePtrValidAgain()
   {
   ...
   auto someFactory = m_dependencyContainer->get<bssh::SomeFactory>();
   assert(someFactory);

   auto nextTask = [this](std::uint32_t dummy){runAfterFoo();};

   m_propBsh_p = someFactory->create(callback, nextTask);
   m_propBsh_p->execute();
   ...
   //and same happens with m_eBsh_p

   return;       
   }

我不确定,使用 gmock/gtest 来避免测试这种简单函数的复杂性的最佳方法是什么,我的目的是获得如上所述的有效指针。

最佳答案

首先,您的ON_CALL 似乎不正确。您模拟了 BshClass,因此您希望对 BshClass 的方法产生期望(toString() 不是)。例如,你应该模拟

ON_CALL(*m_mockEBsh_p, execute())/*stuff to do*/;

回答你的问题:

  1. 如果工厂已经在 Foo 构造函数中使用(您将模拟指针传递给 m_propBsh_pm_eBsh_p),那么您可以只使用 std::shared_ptr 控制它们是否指向某物的功能(例如通过 reset())。
  2. 否则,从 makePtrValidAgain() 中提取工厂方法似乎是个好主意 - 这个函数已经有不止一个职责,它创建指针并调用它们的方法。

我会建议这样的:

   Class Foo : fooParent..
   {
   ...
    void makePtrValidAgain();
    std::string toString();
    ..
    private:
    void resetPointers();
    typedef std::shared_ptr<BshClass> m_propBsh_p;
    typedef std::shared_ptr<BshClass> m_eBsh_p; 
   ...
   }; 

   void Foo::makePtrValidAgain()
   {
   ...
   resetPointers(); //if needed
   m_propBsh_p->execute();
   ...
   //and same happens with m_eBsh_p
   }

   void resetPointers()
   {
   auto someFactory = m_dependencyContainer->get<bssh::SomeFactory>();
   assert(someFactory);

   auto nextTask = [this](std::uint32_t dummy){runAfterFoo();};

   m_propBsh_p = someFactory->create(callback, nextTask);
   //also with the other pointer
   //or better pass the pointer to reset as argument if possible
   }

如果可能,在 Foo 构造函数中调用 resetPointers()。或者将其公开并从 UT 调用它。这样,问题就归结为设置了 m_propBsh_pm_eBsh_p 的第一个选项,您可以通过模拟指针从 UT 访问它们。

关于C++ Gmock - 使用 shared_ptr <FactoryClass> 的测试函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966897/

相关文章:

c++ - 需要一些有关 C++ 中 "swap"函数的帮助

javascript - 尝试通过 Angular Controller 和工厂访问json服务器数据

Python工厂类继承

python - 扭曲的框架需要一些澄清

c# - 工厂模式但带有对象参数

c++ - Qt 上的滚动标签列表

c++ - Microsoft::VisualStudio::CppUnitTestFramework 中的参数化测试方法

Javascript 工厂模式变量作用域

c++ - 在通用容器中查找特定实现

java - 如何在 Java 中正确制作线程安全的单例工厂?