NUnit 无法处理 Lazy Instantiation
NUnit can't cope with Lazy Instantiation
我有一个延迟实例化的单例:
Public Class SingletonBase(Of TDerivedClass As {SingletonBase(Of TDerivedClass)})
Private Shared ReadOnly _instance As New Lazy(Of TDerivedClass)(Function() CType(Activator.CreateInstance(GetType(TDerivedClass), True), TDerivedClass), Threading.LazyThreadSafetyMode.ExecutionAndPublication)
''' <summary>
''' Using the Singleton pattern allows the config handler to be observable
''' </summary>
''' <returns></returns>
Public Shared ReadOnly Property Instance() As TDerivedClass
Get
Return _instance.Value
End Get
End Property
End Class
然后 class 实现它:
Public MustInherit Class ConfigurationHandlerBase(Of TDerivedConfigHandler As {SingletonBase(Of TDerivedConfigHandler)})
Inherits SingletonBase(Of TDerivedConfigHandler)
和实现那个的class:
Public Class ConfigHandler
Inherits ConfigurationHandlerBase(Of ConfigHandler)
Private _test As String
Public Shared ReadOnly Property Test as String
Get
Return Instance._test
End Get
End Property
然后我有一个 NUnit project/classes:
Imports NUnit.Framework
<TestFixture>
Public Class ConfigHandlerTests
<Test>
Public Sub Test()
Dim success As ConfigHandler = ConfigHandler.Instance ' this works
Dim fail as String = ConfigHandler.Test ' this fails!!!
运行 测试 NullReferenceException
和 调试 测试时出现的错误,当它进入 属性 (Dim test As Boolean = ConfigHandler.Instance._test
) 的 Getter 时,将鼠标悬停在 Instance
有时 上会出现以下错误:
'Instance' threw an exception of type 'System.Threading.ThreadAbortException'
如果我尝试进入那行代码,测试总是以以下堆栈跟踪结束:
Result StackTrace:
at NUnit.Framework.Internal.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
at NUnit.Framework.Internal.MethodWrapper.Invoke(Object fixture, Object[] args)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunNonAsyncTestMethod(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.Execute(TestExecutionContext context)
Result Message: Test cancelled by user
我假设 Nunit 与 Lazy
配合得不好 -- 但有人知道如何解决这个问题吗?
原来我是在转移注意力。简单的答案是我需要将配置项放在 NUnit 项目的配置文件中,而不是主项目。
我有一个延迟实例化的单例:
Public Class SingletonBase(Of TDerivedClass As {SingletonBase(Of TDerivedClass)})
Private Shared ReadOnly _instance As New Lazy(Of TDerivedClass)(Function() CType(Activator.CreateInstance(GetType(TDerivedClass), True), TDerivedClass), Threading.LazyThreadSafetyMode.ExecutionAndPublication)
''' <summary>
''' Using the Singleton pattern allows the config handler to be observable
''' </summary>
''' <returns></returns>
Public Shared ReadOnly Property Instance() As TDerivedClass
Get
Return _instance.Value
End Get
End Property
End Class
然后 class 实现它:
Public MustInherit Class ConfigurationHandlerBase(Of TDerivedConfigHandler As {SingletonBase(Of TDerivedConfigHandler)})
Inherits SingletonBase(Of TDerivedConfigHandler)
和实现那个的class:
Public Class ConfigHandler
Inherits ConfigurationHandlerBase(Of ConfigHandler)
Private _test As String
Public Shared ReadOnly Property Test as String
Get
Return Instance._test
End Get
End Property
然后我有一个 NUnit project/classes:
Imports NUnit.Framework
<TestFixture>
Public Class ConfigHandlerTests
<Test>
Public Sub Test()
Dim success As ConfigHandler = ConfigHandler.Instance ' this works
Dim fail as String = ConfigHandler.Test ' this fails!!!
运行 测试 NullReferenceException
和 调试 测试时出现的错误,当它进入 属性 (Dim test As Boolean = ConfigHandler.Instance._test
) 的 Getter 时,将鼠标悬停在 Instance
有时 上会出现以下错误:
'Instance' threw an exception of type 'System.Threading.ThreadAbortException'
如果我尝试进入那行代码,测试总是以以下堆栈跟踪结束:
Result StackTrace:
at NUnit.Framework.Internal.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
at NUnit.Framework.Internal.MethodWrapper.Invoke(Object fixture, Object[] args)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunNonAsyncTestMethod(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.Execute(TestExecutionContext context)
Result Message: Test cancelled by user
我假设 Nunit 与 Lazy
配合得不好 -- 但有人知道如何解决这个问题吗?
原来我是在转移注意力。简单的答案是我需要将配置项放在 NUnit 项目的配置文件中,而不是主项目。