I’m trying to raise an event in a mocked interface. I can get this in C#, but for some pain-in-the-butt reason can’t get it working in VB.Net. If someone could help me out with this situation, I’d appreciate it. Hopefully I haven’t missed the boat conceptually and all I’m missing is some syntax. This is similar to the code I’m working with:
Public Interface ISendable
Event SendMessage(message As String)
End Interface
''**********
Public Interface IPrintable
Sub PrintAnnouncement(announcement As String)
End Interface
'******
Public Class BulletinBoard
Private mPrintable As IPrintable
Public Sub New(sendable As ISendable, printable As IPrintable)
AddHandler sendable.SendMessage, AddressOf GetItOut
mPrintable = printable
End Sub
Public Sub GetItOut(message As String)
'Do some stuff I can verify happened with Moq
mPrintable.PrintAnnouncement(message)
End Sub
End Class
I was hoping to get a test that looked something like this running:
Imports NUnit.Framework
Imports Moq
<TestFixture()> _
Public Class SendMessageTests
<Test()> _
Public Sub canRaiseEvent()
Dim announcement As String = "What the?"
Dim sendable As New Mock(Of ISendable)()
Dim printable As New Mock(Of IPrintable)()
Dim bb As New BulletinBoard(sendable.Object, printable.Object)
'What is the syntax for raising sendable's event?
'sendable.Raise( ....? )
printable.Verify(Sub(d) d.PrintAnnouncement(announcement), Times.Once())
End Sub
End Class
Can anyone help me to complete or correct the line in my test class that begins “sendable.Raise…”? Maybe there is more setup I need to do, but the Moq site didn’t seem to indicate this is the case.
Thanks in advance.
With this line your test is green:
You also need to create a “mock” event handler to make it work:
EDIT:
I’m not a VB guy, so it seams there is a shorter syntax with using an inline anonymous method instead of MockHandler:
: