Created
May 7, 2017 10:22
-
-
Save 121jigowatts/6d89776758aafe7a5c5332f601cf8d7a to your computer and use it in GitHub Desktop.
[C#]privateメソッドの引数のenumをリフレクションでテストに使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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