修改
我做了大量研究并得出结论,我可能想要使用部分模拟。这导致to a question使用我的方法模拟的方法。
问题
当我在方法中使用辅助类时,我通常需要重新实现我的帮助程序类的单元测试(如果不是全部)。这意味着每次更新功能时,我都需要在我使用该功能的每个地方更新测试。它们在很大程度上是多余的(只是一些函数名称/模拟不同)。
任何易于管理的方式?
高度承认的例子
采用这个非常人为的基类示例,提供基本的实用功能:
Public MustInherit Class SomeBase
Public Property WebConnectionService As IWebConnectionService
Public Overridable ReadOnly Property PortNumber As Integer
Get
Return 2352
End Get
End Property
Public Overridable ReadOnly Property Host As String
Get
Return "somesite.com"
End Get
End Property
Protected Sub OpenServiceConnection()
ServiceConnection = WebConnectionService.OpenConnection(Host, PortNumber)
End Sub
End Class
<TestFixture()> _
Public Class SomeBaseTest
Private Class SomeBaseImpl
Inherits SomeBase
Public Overrides ReadOnly Property PortNumber As Integer
Get
Return 1
End Get
End Property
Public Overrides ReadOnly Property Host As String
Get
Return "test.host"
End Get
End Property
Public Sub New(w As IWebConnectionService)
Me.WebConnectionService = w
End Sub
Public Sub Open()
OpenServiceConnection()
End Sub
End Class
<Test()> _
Public Sub OpenServiceConnection_Should_Open_Connection_On_Set_Host_And_Port()
Dim w As IWebConnectionService = MockRepository.GenerateMock(Of IWebConnectionService)()
Dim s As New ServiceBaseImpl(w)
s.Open()
w.AssertWasCalled(Sub(wcs) wcs.OpenConnection(s.Host, s.PortNumber))
End Sub
End Class
当我实现一些实际的基本服务时,我也想要验证连接是否已打开,或许这些行:
Public Class MyClass
Inherits SomeBase
Public Function GetData() As MyDataHolderClass
OpenServiceConnection()
'... other processing
Return data
End Function
End Class
<TestFixture()> _
Public Class MyClassTest
<Test()> _
Public Sub GetData_Should_Connect_To_The_Service()
Dim w As IWebConnectionService = MockRepository.GenerateMock(Of IWebConnectionService)()
Dim s As New MyClass(w)
s.GetData()
w.AssertWasCalled(Sub(wcs) wcs.OpenConnection(s.Host, s.PortNumber))
End Sub
End Class
MS实施的令人敬畏的功能建议
真的,我希望能够编写一个断言使用了帮助程序的测试(因此,帮助程序上的所有传递测试也适用于此处),但这违反了“仅测试代码的外部接口”规则“(我甚至不知道是否可能)。
<Test()> _
Public Sub GetData_Should_Call_OpenConnection_Helper()
Dim s As New ServiceImpl()
s.GetData()
s.AssertCalledOpenConnectionHelper()
End Sub