C++单元测试中的覆盖初始化列表

发布于 2025-02-10 03:10:39 字数 434 浏览 1 评论 0原文

我有一些我想单元测试的C ++代码:

class Example
{
    private: 
        ExpensiveObject expensiveObject;
        
    public:
        Example() : expensiveObject() {
            ... constructor code
        }

        methodA() {
            ... some code
        }
}

Methoda编写单元测试,我需要创建一个示例>示例的实例。问题在于我不想初始化punchobject,我想在单元测试中将其设置为null(我正在使用Google Test)。有办法做到吗?

I have some C++ code like this that I want to unit test:

class Example
{
    private: 
        ExpensiveObject expensiveObject;
        
    public:
        Example() : expensiveObject() {
            ... constructor code
        }

        methodA() {
            ... some code
        }
}

To write a unit test for methodA I need to create an instance of Example. The problem is that I don't want to initialize expensiveObject, I would want to set it to null in my unit test (I am using Google Test). Is there a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2025-02-17 03:10:39

昂贵的观点无法分配null。您可能想要的是将智能指针指向punchobject,并拥有多个构造函数,或者您想更好地注入依赖项。

class Example
{
    private: 
        std::shared_ptr<ExpensiveObject> expensiveObject;
        
    public:
        Example(std::shared_ptr<ExpensiveObject> ptr) : expensiveObject(ptr) {
            //... constructor code
        }

        methodA() {
            //... some code
        }
}

现在您也可以测试无效的情况

Example ex{nullptr};
ex.methodA();

expensiveObject cannot be assigned null. What you might want is to have a smart pointer to ExpensiveObject, and have multiple constructors or better you want to inject your dependencies.

class Example
{
    private: 
        std::shared_ptr<ExpensiveObject> expensiveObject;
        
    public:
        Example(std::shared_ptr<ExpensiveObject> ptr) : expensiveObject(ptr) {
            //... constructor code
        }

        methodA() {
            //... some code
        }
}

Now you can test it for null scenarios as well

Example ex{nullptr};
ex.methodA();
吲‖鸣 2025-02-17 03:10:39

我想不出任何方法,而没有稍微修改示例类的定义。但是,如果您可以忍受这样做,则有几种方法可以做到这一点:使用指针作为另一个答案进行依赖注入,或将类示为类,如下所示:

// Used for production.
class ExpensiveObject {
  // Expensive stuff here
};

// Only used for testing.
class CheapObject {
  // Cheap stuff here
};

// Instantiate with ExpensiveObject for production and with CheapObject for
// testing.
template <class T>
class Example {
 private:
  T expensiveObject;

 public:
  Example() : expensiveObject() {}

  int methodA() { return 1; }
};

TEST(Example, Test1) {
  // CheapObject is used for testing.
  Example<CheapObject> example;
  EXPECT_EQ(example.methodA(), 1);
}

live示例: https://godbolt.org/z/7e1rhwy4b

I can't think of any way to do this without slightly modifying the definition of the Example class. However, if you can tolerate to do this, there are several ways to do this: dependency injection using pointers as the other answer mentioned, or templatizing the class as shown below:

// Used for production.
class ExpensiveObject {
  // Expensive stuff here
};

// Only used for testing.
class CheapObject {
  // Cheap stuff here
};

// Instantiate with ExpensiveObject for production and with CheapObject for
// testing.
template <class T>
class Example {
 private:
  T expensiveObject;

 public:
  Example() : expensiveObject() {}

  int methodA() { return 1; }
};

TEST(Example, Test1) {
  // CheapObject is used for testing.
  Example<CheapObject> example;
  EXPECT_EQ(example.methodA(), 1);
}

Live example: https://godbolt.org/z/7e1rhWY4b

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文