0
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 3 years have passed since last update.

MsTestのprivateメンバ操作

Posted at

テスト対象

/// <summary>
/// テスト対象クラス
/// </summary>
public class TestTargetClass
{
    /// <summary>
    /// Privateメンバ変数
    /// </summary>
    private int _privateMember = 0;

    /// <summary>
    /// PrivateStaticメンバ変数
    /// </summary>
    private static string _privateStaticMember = "";

    /// <summary>
    /// Publicメソッド
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public int PublicMethod(int param)
    {
        return param + 1;
    }

    /// <summary>
    /// Privateメソッド
    /// </summary>
    /// <param name="val1"></param>
    /// <param name="val2"></param>
    /// <returns></returns>
    private int PrivateMethod(int val1, int val2)
    {
        return _privateMember + val1 + val2;
    }

    /// <summary>
    /// Privateメソッド(非同期)
    /// </summary>
    /// <param name="val1"></param>
    /// <param name="val2"></param>
    /// <returns></returns>
    private async Task<int> PrivateMethodAsync(int val1, int val2)
    {
        await Task.Delay(0);
        return val1 + val2;
    }

    /// <summary>
    /// PrivateStaticメソッド
    /// </summary>
    /// <returns></returns>
    private static string GetPrivateStaticMember()
    {
        return _privateStaticMember;
    }
}

テストコード

Publicメソッド

通常ケース

[TestMethod()]
public void PublicMethodTest()
{
    var instance = new TestTargetClass();
    var ret = instance.PublicMethod(1);
    Assert.AreEqual(ret, 2);
}

Privateメソッド+Privateメンバ変数

PrivateObjectのSetFieldOrProperty()でメンバ変数を変更.
またはSetField(),SetProperty().
privateObject.Invoke()でメソッド呼び出し.第2引数以降で呼び出したいメソッドの引数を設定.

[TestMethod()]
public void PrivateMethodTest()
{
    var instance = new TestTargetClass();
    var privateObject = new PrivateObject(instance);

    // メンバ変数値変更
    privateObject.SetFieldOrProperty("_privateMember", 100);
    // Privateメソッド呼び出し
    var ret = privateObject.Invoke("PrivateMethod", 100, 200);
    Assert.AreEqual(ret, 300);
}

Privateメソッド(非同期)

呼び出しを戻り値の型でくるんでawait.

[TestMethod()]
public async Task PrivateMethodAsyncTest()
{
    var instance = new TestTargetClass();
    var privateObject = new PrivateObject(instance);
    // 戻り値の型でくるんでawait
    var ret = await (privateObject.Invoke("PrivateMethodAsync", 100, 200) as Task<int>);
    Assert.AreEqual(ret, 300);
}

PrivateStaticメソッド+PrivateStaticメンバ変数

PrivateTypeのSetStaticFieldOrProperty()とInvokeStatic()を使用.
基本は通常のPrivateのときと同じ.

[TestMethod()]
public void GetPrivateStaticMemberTest()
{
    var privateType = new PrivateType(typeof(TestTargetClass));
    // static変数値変更
    privateType.SetStaticFieldOrProperty("_privateStaticMember", "ヨシッ!");
    // staticメソッド呼び出し
    var ret = privateType.InvokeStatic("GetPrivateStaticMember");

    Assert.AreEqual(ret, "ヨシッ!");
}
0
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
0
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?