I want to test the following two (unrelated) methods and achieve full branch and statement coverage using OpenCover 2.0.802.1
public class Methods
{
public static void MethodWithDelegate(SynchronizationContext context)
{
context.Send(delegate { Console.Beep(); }, null);
}
public static string MethodWithSwitchStatement(Type value)
{
string output = string.Empty;
if (value != null)
{
switch (value.ToString())
{
case "System.Int32":
output = "int";
break;
default:
output = "other type";
break;
}
}
return output;
}
}
I have written the following (NUnit) tests, one using a 'Moq' mock object:
[Test]
public void Test_ShouldCoverMethodWithDelegate()
{
var context = new Mock<SynchronizationContext>();
Methods.MethodWithDelegate(context.Object);
context.Verify(x => x.Send(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()));
}
[Test]
public void Test_ShouldCoverSwitchStatement()
{
Assert.That(Methods.MethodWithSwitchStatement(null), Is.EqualTo(string.Empty));
Assert.That(Methods.MethodWithSwitchStatement(typeof(int)), Is.EqualTo("int"));
Assert.That(Methods.MethodWithSwitchStatement(typeof(float)), Is.EqualTo("other type"));
}
However, after running the tests through OpenCover, the coverage.xml
file always contains a branch point with a zero visit count for both tests. The sequence coverage shows 100%.
Not being an IL expert, I'm not sure how I'd go about writing further tests to get the branch coverage to 100%.