c++ - 在 Visual Studio 中测试时如何覆盖标准 C++ 函数?

标签 c++ visual-studio unit-testing mocking shim

我正在使用 Visual Studio 2013 单元测试。我的代码使用 time() 函数来生成一些对象名称,因此在测试时很难确保一致的行为。

如果是 C#,我可以使用垫片,如文章 http://msdn.microsoft.com/en-us/library/hh549175.aspx 中所示“垫片入门”部分。

有什么技巧可以在我的 C++ 单元测试期间重新定义 time() 调用吗?

最佳答案

你想做的是模拟(http://en.wikipedia.org/wiki/Mock_object)。

我个人喜欢使用 Hippomocks ( https://github.com/dascandy/hippomocks ),它允许在当前版本中模拟 C 和 C++ 函数(非虚拟类方法是一个明显的异常(exception),例如 Mocking non-virtual methods in C++ without editing production code? )。

关于c++ - 在 Visual Studio 中测试时如何覆盖标准 C++ 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164704/

相关文章:

unit-testing - 单元测试 : private methods and how to refactor

c++ - 为什么不能简单初始化(带大括号)2D std::array?

c++ - 创建 boost 工具可执行文件

c# - 最新 windows 10 更新后 Visual Studio 2013 发布崩溃

unit-testing - angularjs - 测试 Controller

PHPUnit:如何测试远程 Postgres 服务器上的数据库交互?

c++ - 从整数范围内搜索

c++ - 如何从动态内存中正确删除对象的指针?

c# - 在抽象通用测试类中定义单元测试

c++ - 如何将 Visual Studio 中的“查找所有引用”仅限于相关变量