我正在尝试为 TPL BufferBlock 创建单元测试,并希望测试是否引发了异常。但是,测试在抛出异常之前通过。
编辑
此外,这是一个长期运行的过程,所以我不称之为完成。此过程一直运行到应用程序关闭
这是代码:
public class PriorityMessageQueue
{
private BufferBlock<MyMessage> _messageBufferBlock;
private async Task<bool> Init()
{
var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 1000
};
var prioritizeMessageBlock = new ActionBlock<MyMessage>(msg =>
{
try
{
SetMessagePriority(msg);
}
catch (Exception)
{
throw;
}
});
_messageBufferBlock = new BufferBlock<MyMessage>(executionDataflowBlockOptions);
_messageBufferBlock.LinkTo(prioritizeMessageBlock);
}
public async Task<bool> EnqueueAsync(MyMessage message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message), "The message object is NULL. Cannot enqueue a NULL object.");
}
return await _messageBufferBlock.SendAsync(message);
}
private void SetMessagePriority(MyMessage message)
{
if (message.MessageType.Equals(MessageType.Unknown))
{
throw new ArgumentException("The SCBA Message Type is 'Unknown'. Cannot set the Message Priority on an 'Unknown' message type.");
}
switch (message.MessageType)
{
case MessageType.Admin:
message.MessagePriority = MessagePriority.Admin;
break;
case MessageType.AuthUser:
message.MessagePriority = MessagePriority.High;
break;
case MessageType.UnknownUser:
message.MessagePriority = MessagePriority.Normal;
break;
default:
message.MessagePriority = MessagePriority.Normal;
break;
}
}
}
这是测试代码
[TestClass()]
public class PriorityMessageQueueTests
{
private IPriorityMessageQueue _queue;
[TestInitialize]
public void Init()
{
IUnityContainer container = new UnityContainer();
var logRepository = new Mock<ILogRepository>();
container.RegisterInstance(logRepository.Object);
_queue = new PriorityMessageQueue(logRepository.Object);
}
[TestCleanup]
public void CleanUp()
{
_queue.Dispose();
}
[TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public async Task EnqueueTest_NullMessage()
{
await _queue.EnqueueAsync(null);
}
[TestMethod()]
public async Task EnqueueTest_UnknownMessageType()
{
var message = new MyMessage
{
Id = 1,
Text = "Unit Test"
};
var result = await _queue.EnqueueAsync(message);
Assert.AreEqual(true, result);
}
[TestMethod()]
public void DequeueTest()
{
Assert.Fail();
}
[TestMethod()]
public void GetNextInQueue()
{
Assert.Fail();
}
}
ArgumentException 异常在 SetMessagePriority 中被正确抛出,因为“MessageType”等于 MessageType.Unknown。但是,在引发 ArgumentException 时,单元测试 EnqueueTest_UnknownMessageType 已成功通过,因为
var result = await _queue.EnqueueAsync(message);
在抛出异常之前返回“true”。如何编写测试 EnqueueTest_UnknownMessageType 以使其因抛出异常而失败?
我试过添加
[ExpectedException(typeof(ArgumentException))]
测试,但它仍然通过