循环引用时 .NET 单元测试中的 StackOverflow
StackOverflow in .NET unit testing when references are circular
当我注意到:
public class Foo
{
private Bar myBar = new Bar();
}
public class Bar
{
private Foo myFoo = new Foo();
}
[Fact]
public void CircularReferenceTest()
{
var foo = new Foo();
var bar = new Bar();
}
导致 XUnit 运行器停止和控制台日志:
The active test run was aborted. Reason: Process is terminated due to WhosebugException.
我在 MStest 上测试了它,得到了相同的结果。有没有解决的办法?这是一个错误,还是打算以这种方式停止执行?
您没有进行循环引用。你正在制作一堆指向另一个的引用(如果你说是链表),最终它会导致堆栈溢出异常,因为堆栈已满。
下面是循环引用的方法。我认为您不能将字段设为私有,因为两个 类 必须在某个时候以某种方式相互认识。 (即在某些时候必须建立这种联系)
public class Foo
{
public Bar MyBar { get; set; }
}
public class Bar
{
public Foo MyFoo { get; set; }
}
public void CircularReferenceTest()
{
var foo = new Foo();
var bar = new Bar();
foo.MyBar = bar;
bar.MyFoo = foo;
}
我也遇到了这个问题:Visual Studio只是悄悄停止了测试运行,结果没有定论,也没有查明是什么原因造成的。它只是用蓝色图标停止了测试,表示结果不确定。在输出 window 中,我注意到相同的错误消息:
The active test run was aborted. Reason: Process is terminated due to WhosebugException.
解决方案是 运行 测试为 "Debug Selected Test"。 Visual Studio 然后突出显示参与循环引用循环的行之一。应该在该行上放置一个 break-point 并再次调试测试。从这一点开始,调试器将单步执行循环引用路径。
当我注意到:
public class Foo
{
private Bar myBar = new Bar();
}
public class Bar
{
private Foo myFoo = new Foo();
}
[Fact]
public void CircularReferenceTest()
{
var foo = new Foo();
var bar = new Bar();
}
导致 XUnit 运行器停止和控制台日志:
The active test run was aborted. Reason: Process is terminated due to WhosebugException.
我在 MStest 上测试了它,得到了相同的结果。有没有解决的办法?这是一个错误,还是打算以这种方式停止执行?
您没有进行循环引用。你正在制作一堆指向另一个的引用(如果你说是链表),最终它会导致堆栈溢出异常,因为堆栈已满。
下面是循环引用的方法。我认为您不能将字段设为私有,因为两个 类 必须在某个时候以某种方式相互认识。 (即在某些时候必须建立这种联系)
public class Foo
{
public Bar MyBar { get; set; }
}
public class Bar
{
public Foo MyFoo { get; set; }
}
public void CircularReferenceTest()
{
var foo = new Foo();
var bar = new Bar();
foo.MyBar = bar;
bar.MyFoo = foo;
}
我也遇到了这个问题:Visual Studio只是悄悄停止了测试运行,结果没有定论,也没有查明是什么原因造成的。它只是用蓝色图标停止了测试,表示结果不确定。在输出 window 中,我注意到相同的错误消息:
The active test run was aborted. Reason: Process is terminated due to WhosebugException.
解决方案是 运行 测试为 "Debug Selected Test"。 Visual Studio 然后突出显示参与循环引用循环的行之一。应该在该行上放置一个 break-point 并再次调试测试。从这一点开始,调试器将单步执行循环引用路径。