编辑
我进行了大量研究并得出结论,我可能想为此使用部分模拟。这导致了一个问题,即在我的方法模拟中使用什么方法。
问题
当我在方法中使用辅助类时,我通常需要重新实现很多(如果不是全部)我的辅助类的单元测试。这意味着每次更新功能时,我都需要在使用该功能的每个地方更新测试。它们在很大程度上是多余的(只是一些函数名称/模拟不同)。
有什么容易管理的方法吗?
高度人为的例子
以这个提供基本实用功能的基类的高度设计示例为例:
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