我想在 Visual Studio native 单元测试项目中测试一些编写为在嵌入式处理器上运行的代码。
TestMe 类有几个适合测试的方法,但 Foo 和 Bar 类直接访问仅在嵌入式处理器上可用的内存映射寄存器。
#pragma once
#include "Foo.h"
#include "Bar.h"
class TestMe
{
public:
TestMe(Foo& aFoo, Bar& aBar);
~TestMe();
float FooBar();
};
模拟掉这些对象以便我可以测试 TestMe 类的最佳方法是什么?
编辑:对我来说,最好的方法是尽可能少地干扰正在测试的软件。
最佳答案
“最佳”总是主观的,但我喜欢使用模板进行这种模拟:
template <typename TFoo, typename TBar>
class TestMeImpl
{
public:
TestMeImpl(TFoo& aFoo, TBar& aBar);
~TestMeImpl();
float FooBar();
};
using TestMe = TestMeImpl<Foo, Bar>;
您将针对 TestMeImpl
编写单元测试,但将 TestMe
暴露给您的用户。
关于c++ - 模拟被测类的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52478352/