tests/DotNetCommon.Test/Extensions/TaskExtensions/TaskExceptionsTests.cs
(开头部分) 10KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
namespace DotNetCommon.Test.Extensions.TaskExtensions
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Shouldly;
using DotNetCommon.Extensions;
[TestFixture]
public class TaskExceptionsTests
{
[Test]
public void ShouldHandleExpectedExceptionWhenNoException()
{
var exceptionsQueue = new Queue<Exception>();
Action action = () =>
{
var t = Task.Factory.StartNew(() =>
{
try { throw new InvalidOperationException(); } catch (Exception) { /* Ignore */ }
}).HandleException<InvalidOperationException>(e => { exceptionsQueue.Enqueue(e); });
t.Wait();
};
action.ShouldNotThrow();
exceptionsQueue.ShouldBeEmpty();
}
[Test]
public void ShouldHandleExpectedExceptionNonPresentException()
{
var exceptionsQueue = new Queue<Exception>();
Action action = () =>
{
var t = Task.Factory.StartNew(() => { throw new DivideByZeroException(); })
.HandleException<InvalidOperationException>(
e => { exceptionsQueue.Enqueue(e); });
t.Wait();
};
action.ShouldThrow<AggregateException>()
.Flatten().InnerExceptions
.Count(e => e.GetType() == typeof(DivideByZeroException) && e.Message == "Attempted to divide by zero.")
.ShouldBe(1);
exceptionsQueue.ShouldBeEmpty();
}
[Test]
public void ShouldHandleExpectedExceptionSingleException()
{
后面还有 249 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
