gpt4 book ai didi

c# - 遗留代码以某种方式对 ThreadAbortException 使用react的单元测试

转载 作者:行者123 更新时间:2023-11-30 14:50:00 26 4
gpt4 key购买 nike

我有一些现有的遗留代码,我想对其进行测试。这是要点的复制:

public class LegacyUnit
{
private readonly ICollaborator collaborator;

public LegacyUnit(ICollaborator collaborator)
{
this.collaborator = collaborator;
}

public object GetStuff(HttpContextBase context, string input)
{
try
{
if (input == "")
{
context.Response.End();
}

collaborator.DoOtherStuff();

return "Done!";
}
catch (ThreadAbortException)
{ }

return null;
}
}

现在,这个遗留单元有一些问题,但现在我只是想对其进行测试。具体来说,我想测试如果 Response.End() 引发了 ThreadAbortcollaborator.DoOtherStuff 不会被调用.

问题:如何引发这样的异常?

我已通读 this question and its answers on ThreadAbortException ,并了解它的特殊性。但是,我没有从这些帖子中看到如何在单元测试中处理这个问题。

这是我的尝试:

[Test]
public void DoesNotCallCollaboratorOnThreadAbort()
{
var testResponseMock = new Mock<HttpResponseBase>();
var testContextMock = new Mock<HttpContextBase>();
var collaboratorMock = new Mock<ICollaborator>();

testContextMock.Setup(x => x.Response).Returns(testResponseMock.Object);
testResponseMock.Setup(x => x.End()).Throws<ThreadAbortException>(); // Compile error

var unit = new LegacyUnit(collaboratorMock.Object);
unit.GetStuff(testContextMock.Object, "");

collaboratorMock.Verify(c => c.DoOtherStuff(), Times.Never);
}

显然编译器会提示:ThreadAbortException 没有可用的构造函数。此外,它是密封的(可能出于充分的原因),因此创建“可测试”子类将不起作用。

测试此类代码的正确方法是什么?它是否可行,或者 LegacyUnit 是否对测试太不友好?


完整的最小重现(带有 NUnit 2.6.4 和 Moq 4.5.9 的空 .NET 4.5 类库):

public interface ICollaborator
{
void DoOtherStuff();
}

public class LegacyUnit
{
private readonly ICollaborator collaborator;

public LegacyUnit(ICollaborator collaborator)
{
this.collaborator = collaborator;
}

public object GetStuff(HttpContextBase context, string input)
{
try
{
if (input == "") context.Response.End();
collaborator.DoOtherStuff();
return "Done!";
}
catch (ThreadAbortException)
{ }

return null;
}
}

[TestFixture]
public class LegacyUnitTests
{
[Test]
public void DoesNotCallCollaboratorOnThreadAbort()
{
var testResponseMock = new Mock<HttpResponseBase>();
var testContextMock = new Mock<HttpContextBase>();
var collaboratorMock = new Mock<ICollaborator>();

testContextMock.Setup(x => x.Response).Returns(testResponseMock.Object);
testResponseMock.Setup(x => x.End()).Throws<ThreadAbortException>(); // Compile error here

var unit = new LegacyUnit(collaboratorMock.Object);
unit.GetStuff(testContextMock.Object, "");

collaboratorMock.Verify(c => c.DoOtherStuff(), Times.Never);
}
}

最佳答案

ThreadAbortException 通过在目标线程上调用 Abort 引发。您可以创建一个线程来运行测试并在 testResponseMock.End 的 mock 中调用 Abort,例如

testContextMock.Setup(x => x.Response).Returns(testResponseMock.Object);

var unit = new LegacyUnit(collaboratorMock.Object);
var thread = new Thread(() => unit.GetStuff(testContextMock.Object, ""));

testResponseMock.Setup(x => x.End()).Callback(() => { Thread.CurrentThread.Abort(); });

thread.Start();
thread.Join();

collaboratorMock.Verify(c => c.DoOtherStuff(), Times.Never);

关于c# - 遗留代码以某种方式对 ThreadAbortException 使用react的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37832079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com