次の2つの(無関係な)メソッドをテストし、OpenCover2.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;
}
}
次の(NUnit)テストを作成しました。1つは「Moq」モックオブジェクトを使用しています。
[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"));
}
ただし、OpenCoverを介してテストを実行した後、coverage.xml
ファイルには常に、両方のテストの訪問数がゼロの分岐点が含まれます。シーケンスカバレッジは100%を示しています。
ILの専門家ではないので、ブランチカバレッジを100%にするために、さらにテストを作成する方法がわかりません。