小编Mas*_*asD的帖子

C++单元测试遗留代码:如何处理#include?

我刚刚开始使用#include指令为具有大型物理依赖性的遗留代码模块编写单元测试.我一直在处理它们的一些感觉过于繁琐的方法(提供空标题来打破长#include依赖列表,并使用#define来防止编译类)并且正在寻找一些更好的策略来处理这些问题.

我经常遇到几乎每个头文件都复制一个空白版本的问题,以便将我正在测试的类分开,然后为需要的对象编写大量的存根/模拟/伪代码取而代之,因为他们现在尚未定义.

谁知道一些更好的做法?

c++ legacy unit-testing

17
推荐指数
1
解决办法
2838
查看次数

编译错误:使用对象引用的函数参数被构造对象混淆

在类的单元测试中,我尝试通过显式调用空构造函数来声明一个类变量,并将其传递给一个函数,该函数除了对我声明的类型的接口的引用之外,但编译器会产生错误.当我在没有任何显式构造函数调用的情况下声明它时,函数接受它.

请参阅以下代码:

//classundertest.h
class IController;

class CClassUnderTest
{
public:
    CClassUnderTest() {}
    virtual ~CClassUnderTest() {}

    unsigned int Run(IController & controller);
};

//testclassundertest.h
#include "UnitTest++.h"

#include "classundertest.h"
#include "icontroller.h"

class CTestController : public IController
{
public:
    CTestController() : IController() {}
    virtual ~CTestController() {}

    virtual void Play(unsigned int i) {}
};

struct CClassUnderTestFixture
{
    CClassUnderTest classUnderTest;
};

TEST_FIXTURE(CClassUnderTestFixture, RunTest)
{
    CTestController controllerA;   

    CHECK_EQUAL(classUnderTest.Run(controllerA), 105U);

    CTestController controllerB();   

    CHECK_EQUAL(classUnderTest.Run(controllerB), 105U);
}
Run Code Online (Sandbox Code Playgroud)

编译器认为controllerB是构造函数的引用:

错误:没有匹配函数来调用`CClassUnderTest :: Run(CTestController(&)())'错误:候选者是:unsigned int CClassUnderTest :: Run(IController&)

我很困惑为什么编译器在实例化controllerB时不允许我调用构造函数,特别是当生产代码看起来好吗?

c++ compiler-errors reference

0
推荐指数
1
解决办法
135
查看次数

标签 统计

c++ ×2

compiler-errors ×1

legacy ×1

reference ×1

unit-testing ×1