1

我正在尝试测试接口和控制器之间的事件处理程序是否正确连接。该系统的设置如下例所示:

'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

如何使用布尔参数引发事件,以便我可以测试事件处理程序中包含的每个代码路径。我对 Google 代码主页上显示的语法没有任何运气。

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
4

1 回答 1

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文档中看到更多信息,但诀窍是将委托类型作为类型参数提供给Raise.With.

于 2011-04-28T11:15:57.653 回答