我试图测试接口和控制器之间的事件处理程序是否正确连接。系统设置如下:
'Interface for Display
Public Interface IClientLocationView
Event LocationChanged(ishomelocation as Boolean)
Sub DisplayChangesWhenHome(arg1 as Object, arg2 as Object)
Sub DisplayChangesWhenNotHome(arg1 as Object, arg2 as Object, arg3 as Object)
End Interface
'Controller
Public Class ClientLocationController
Public Sub Start(_view as IClientLocationView)
AddHandler _view.LocationChanged, AddressOf LocationChangedHandler
End Sub
Public Sub LocationChangedHandler(ishomelocation as Boolean)
If ishomelocation Then
_view.DisplayChangesWhenHome(arg1, arg2)
Else
_view.DisplayChangesWhenNotHome(arg2, arg1, arg3)
End If
End Sub
End Class
如何使用boolean参数引发事件,以便我可以测试事件处理程序中包含的每个代码路径。我对Google Code主页上显示的语法没有任何好运。
AddHandler foo.SomethingHappened, AddressOf Raise.With(EventArgs.Empty).Now
'If the event is an EventHandler(Of T) you can use the shorter syntax:'
AddHandler foo.SomethingHappened, Raise.With(EventArgs.Empty).Go
这是我到目前为止所做的:
<TestMethod()>
Public Sub VerifyThat_LocationChangedHandler_IsWired()
Dim _view As IClientLocationView= A.Fake(Of IClientLocationView)()
Dim pres As ClientLocationController = A.Fake(Of ClientLocationController)(Function() New ClientLocationController(_view))
pres.Start()
'?????? Need to raise the event
'AddHandler _view.LocationChanged, AddressOf Raise.With(EventArgs.Empty).Now
End Sub
答案 0 :(得分:2)
使用早于2.0.0的FakeItEasy版本时,事件应采用EventHandler委托的形式:
Event LocationChanged As EventHandler(Of LocationChangedEventArgs)
Public Class LocationChangedEventArgs
Inherits EventArgs
Public Property IsHomeLocation As Boolean
End Class
一旦更改,您现在可以使用事件提升语法:
AddHandler _view.LocationChanged, Raise.With(New LocationChangedEventArgs With { .IsHomeLocation = True })
自FakeItEasy 2.0.0起,此限制不再适用。您可以在Raising Events文档中查看更多内容,但诀窍是将委托类型作为typeparam提供给Raise.With
。