3

如何让 VS TEST_CLASS(SomeTestClassName) 成为我正在测试的班级的朋友?我是 C++ 新手,在网上找不到任何信息。如果是简单的添加

friend class SomeTestClassName;

到被测试的类的标题 - 这不起作用。

添加示例:

#include "..\CoreTests\ChallengeManagerTests.cpp" //Circular reference? But forward declaration don't works

class ChallengeManager
{
private:
 void PrivateMethod();
public:
 void PublicMethod();
 friend class CoreTests::ChallengeManagerTests; // Compiler can't find ChallengeManagerTests in CoreTests namespace. Forward declaration give class redefinition error.
}

//ChallengeManagerTests:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\Core\ChallengeManager.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Brans;
namespace CoreTests
{
    TEST_CLASS(ChallengeManagerTests)
    {
    public:

        TEST_METHOD(RandomInputs)
        {
            Brans::ChallengeManager cm;
            cm. // here i got is not accessible error for private members 

        }

    };
}
4

2 回答 2

2

从您删除的上一个问题来看,您的 testclass 名称是在命名空间内定义的,因此您需要在朋友声明中提供相同的命名空间。

于 2013-10-22T14:31:53.093 回答
1

只需在类 ChallengeManager 定义之前添加测试类的前向声明,如下所示:

class CoreTests::ChallengeManagerTests;
于 2016-08-01T12:40:57.487 回答