单元测试单元测试 - C ++? [英] How to unit test Singleton class - C++?

查看:190
本文介绍了单元测试单元测试 - C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中单元测试单元格式的方法是什么? (附例子)

What are the ways to unit test a Singleton pattern in C++? (with examples please)

推荐答案

使单例执行成为一个单独的类,并使包装器实现单身外。这样,您可以尽可能多地测试实现(除了单纯行为是不必要的。

Make the implementation of the singleton a separate class, and make a wrapper that implements the "singletonness" outside. That way you can test the implementation as much as you like (except the singleton behavior which is trivial and unnecessary.

class SingletonImpl {
public:
  int doit(double,double);
};

class Singleton {
public:
  Singleton& instance() {...}
  int doit(double a,double b) {impl->doit(a,b);}
  ...
private:
  SingletonImpl impl;
}

这篇关于单元测试单元测试 - C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆