У меня есть следующий метод:Может ли этот метод достичь покрытия на 100% кода?
public static async Task<bool> CreateIfNotExistsAsync(
this ISegment segment,
CancellationToken cancellationToken)
{
Requires.ArgNotNull(segment, nameof(segment));
try
{
await segment.CreateAsync(cancellationToken);
return true;
}
catch (Exception error)
{
if (error.CanSuppress() && !cancellationToken.IsCancellationRequested)
{
var status = await segment.GetStatusAsync(cancellationToken);
if (status.Exists)
{
return false;
}
}
throw;
}
}
... для которого я написал тесты, которые должны охватывать все блоки. Однако; Результаты покрытия кода (в Visual Studio 2015 Update 3) показывает, что два блока не распространяется на:
Я полагал, что это было что-то делать с кодом, который генерируется для await
внутри броской блока, поэтому я попытался переписывания метод, как это:
public static async Task<bool> CreateIfNotExistsAsync(
this ISegment segment,
CancellationToken cancellationToken)
{
Requires.ArgNotNull(segment, nameof(segment));
ExceptionDispatchInfo capturedError;
try
{
await segment.CreateAsync(cancellationToken);
return true;
}
catch (Exception error)
{
if (error.CanSuppress() && !cancellationToken.IsCancellationRequested)
{
capturedError = ExceptionDispatchInfo.Capture(error);
}
else
{
throw;
}
}
var status = await segment.GetStatusAsync(cancellationToken);
if (!status.Exists)
{
capturedError.Throw();
}
return false;
}
Однако, есть еще один блок, который не охватывается:
Можно ли переписать этот метод так, чтобы он мог быть полностью покрыт?
Вот мои родственные испытания:
[TestMethod]
public async Task Create_if_not_exists_returns_true_when_create_succeed()
{
var mock = new Mock<ISegment>();
Assert.IsTrue(await mock.Object.CreateIfNotExistsAsync(default(CancellationToken)));
mock.Verify(_ => _.CreateAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[TestMethod]
public async Task Create_if_not_exists_throws_when_create_throws_and_cancellation_is_requested()
{
var mock = new Mock<ISegment>();
var exception = new Exception();
mock.Setup(_ => _.CreateAsync(It.IsAny<CancellationToken>())).Throws(exception);
try
{
await mock.Object.CreateIfNotExistsAsync(new CancellationToken(true));
Assert.Fail();
}
catch (Exception caught)
{
Assert.AreSame(exception, caught);
}
mock.Verify(_ => _.CreateAsync(It.IsAny<CancellationToken>()), Times.Once);
mock.Verify(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()), Times.Never);
}
[TestMethod]
public async Task Create_if_not_exists_throws_when_create_throws_non_suppressable_exception()
{
var mock = new Mock<ISegment>();
var exception = new OutOfMemoryException();
mock.Setup(_ => _.CreateAsync(It.IsAny<CancellationToken>())).Throws(exception);
try
{
await mock.Object.CreateIfNotExistsAsync(default(CancellationToken));
Assert.Fail();
}
catch (Exception caught)
{
Assert.AreSame(exception, caught);
}
mock.Verify(_ => _.CreateAsync(It.IsAny<CancellationToken>()), Times.Once);
mock.Verify(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()), Times.Never);
}
[TestMethod]
public async Task Create_if_not_exists_throws_when_create_throws_and_status_says_segment_doesnt_exists()
{
var mock = new Mock<ISegment>();
var exception = new Exception();
mock.Setup(_ => _.CreateAsync(It.IsAny<CancellationToken>())).Throws(exception);
mock.Setup(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new SegmentStatus(false, false, null, 0));
try
{
await mock.Object.CreateIfNotExistsAsync(default(CancellationToken));
Assert.Fail();
}
catch (Exception caught)
{
Assert.AreSame(exception, caught);
}
mock.Verify(_ => _.CreateAsync(It.IsAny<CancellationToken>()), Times.Once);
mock.Verify(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[TestMethod]
public async Task Create_if_not_exists_returns_false_when_create_throws_and_status_says_segment_exists()
{
var mock = new Mock<ISegment>();
mock.Setup(_ => _.CreateAsync(It.IsAny<CancellationToken>())).Throws<Exception>();
mock.Setup(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new SegmentStatus(true, false, null, 0));
Assert.IsFalse(await mock.Object.CreateIfNotExistsAsync(default(CancellationToken)));
mock.Verify(_ => _.CreateAsync(It.IsAny<CancellationToken>()), Times.Once);
mock.Verify(_ => _.GetStatusAsync(It.IsAny<CancellationToken>()), Times.Once);
}
Это CanSuppress
-logic:
private static readonly Exception[] EmptyArray = new Exception[0];
/// <summary>
/// Determines whether an <see cref="Exception"/> can be suppressed.
/// </summary>
/// <param name="exception">
/// The <see cref="Exception"/> to test.
/// </param>
/// <returns>
/// <c>true</c> when <paramref name="exception"/> can be suppressed; otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// <para>
/// We do not want to suppress <see cref="OutOfMemoryException"/> or <see cref="ThreadAbortException"/>
/// or any exception derived from them (except for <see cref="InsufficientMemoryException"/>, which we
/// do allow suppression of).
/// </para>
/// <para>
/// An exception that is otherwise suppressable is not considered suppressable when it has a nested
/// non-suppressable exception.
/// </para>
/// </remarks>
public static bool CanSuppress(this Exception exception)
{
foreach (var e in exception.DescendantsAndSelf())
{
if ((e is OutOfMemoryException && !(e is InsufficientMemoryException)) ||
e is ThreadAbortException)
{
return false;
}
}
return true;
}
private static IEnumerable<Exception> DescendantsAndSelf(this Exception exception)
{
if (exception != null)
{
yield return exception;
foreach (var child in exception.Children().SelectMany(ExceptionExtensions.DescendantsAndSelf))
{
yield return child;
}
}
}
private static IEnumerable<Exception> Children(this Exception parent)
{
DebugAssumes.ArgNotNull(parent, nameof(parent));
var aggregate = parent as AggregateException;
if (aggregate != null)
{
return aggregate.InnerExceptions;
}
else if (parent.InnerException != null)
{
return new[] { parent.InnerException };
}
else
{
return ExceptionExtensions.EmptyArray;
}
}
На первом снимке, на котором блоки не покрываются ..? – Rob
Я предполагаю, что синий означает покрытый; Оранжевый означает не охваченный; Purpleish означает частично покрытый. Поэтому я бы сказал 'throw;' и '}', хотя 'throw' ** ** покрыт, а'} 'даже не должен содержать код ... –
' '' не является чем-то «закрываемым», и я предполагаю, что он не подсвечен, потому что код никогда не выпадает из этой области (что хорошо - то же самое происходит после операторов возврата). Что касается броска, это не имеет ничего общего со структурой, потому что исключение является подавляемым, и задача не отменяется. Итак, вам нужно написать новый тест (с исходным кодом), который вызывает исключение, исключающее исключение, или отменить задачу – Rob