Skip to content

Instantly share code, notes, and snippets.

@121jigowatts
Created May 7, 2017 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 121jigowatts/6d89776758aafe7a5c5332f601cf8d7a to your computer and use it in GitHub Desktop.
Save 121jigowatts/6d89776758aafe7a5c5332f601cf8d7a to your computer and use it in GitHub Desktop.
[C#]privateメソッドの引数のenumをリフレクションでテストに使う
using System;
namespace sample
{
public class Class1
{
private string GetValue(MyEnum em)
{
if (Enum.IsDefined(typeof(MyEnum), em))
{
return ((MyEnum)em).ToString();
}
return string.Empty;
}
}
enum MyEnum
{
first,
second,
third,
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
namespace sample.Test
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var a = Assembly.Load("sample");
var type = a.GetType("sample.MyEnum");
//var underlyingType = Enum.GetUnderlyingType(type);
var enumValues = Enum.GetValues(type);
var value = enumValues.GetValue(0);
//var ret = Convert.ChangeType(value, underlyingType);
var target = new Class1();
var po = new PrivateObject(target);
var arg = Convert.ChangeType(value, type);
var args = new object[] { arg };
var expected = "first";
var actual = po.Invoke("GetValue", args);
Assert.AreEqual(expected, actual);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment