4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WinUnitでテスト対象クラスのprivateメンバにアクセスする方法

Posted at

テスト対象のTargetクラスがあります。
friendでprivateのメンバにアクセスできるテスト用のクラスを宣言します。

Target.h
class Target {
private:
    friend class TargetTest;
    int m_value;
};

テスト用のTargetTestクラスはこうなります。
usingを使ってprivateなメンバにアクセスできるように定義しています。

TargetTest.h
#include <Target.h>
class TargetTest : public Target {
public:
    using Target::m_value;
};

TargetTestクラス使えば、Targetクラスと同じ記述でTargetクラスのメンバにアクセスでるようになります。

#include "stdafx.h"
#include "TargetTest.h"

BEGIN_TEST(Test1)
{
    TargetTest obj;
    obj.m_value = 10;
    WIN_ASSERT_EQUAL(10, obj.m_value);
}
END_TEST
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?