hello,
I try to create a mock for System.Enum. That works for all public static methods of the System.Enum class (i.e.
(all C# .NET)
method:
mock:
//--- this causes an exception (Cannot create mock for type due to CLR limitations.)
I try to create a mock for System.Enum. That works for all public static methods of the System.Enum class (i.e.
Enum.GetValues
). But I have no idea how to set up the mock for the ToString() method of the Enum object.(all C# .NET)
method:
public
static
void
TestThis
(ListControl ctrl, Type enumType, Enum enumItem=
null
)
{
int
[] itemValues = Enum.GetValues(enumType).ToArray<
int
>();
string
[] itemNames = Enum.GetNames(enumType);
string
s = enumItem.ToString();
}
mock:
Mock.SetupStatic(
typeof
(Enum), Behavior.Strict);
Mock.Arrange(() => Enum.GetValues(Arg.IsAny<Type>())).Returns(
new
int
[] { 1, 2, 3 });
Mock.Arrange(() => Enum.GetNames(Arg.IsAny<Type>())).Returns(
new
string
[] {
"1"
,
"2"
,
"3"
});
//--- this causes an exception (Cannot create mock for type due to CLR limitations.)
Enum e = Mock.Create<Enum>();
Mock.Arrange(() => e.ToString()).IgnoreArguments().IgnoreInstance().Returns(
"2"
);
//---
//--- in that case null is returned instead of "2"; compiler never steps into the mocked "returns"
TestEnum
e = Mock.Create<TestEnum>();
Mock.Arrange(() => e.ToString()).IgnoreArguments().IgnoreInstance().Returns(
"2"
);
//---
DropDownList ddlTestList = new DropDownList();
TestThis(ddlTestList, typeof(TestEnum), TestEnum.Empty
)