-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling .Raise raises the event on a new instance of the mocked object #1221
Comments
Please post minimal but complete repro code. You've excluded the required bits of the definition of |
Actually I think I realize that the way im using this test is wrong. But ill post the code here to see if you guys propose a way to raise such event on the abstract class to test the base code: Public MustInherit Class MyAbstractClass
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub New()
Console.WriteLine("MyAbstractClass Ctor called.")
End Sub
Public Property MyProperty1 As Double
Private Sub MyAbstractTestClass_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles Me.PropertyChanged
MyProperty1 = 100
End Sub
End Class
<TestClass()>
Public Class MyAbstractClassTest
<TestMethod>
Public Sub MyAbstractClass_OnPropertyChanged_MyProperty1ValueChanges()
'
' Arrange.
'
Dim mockTestObject = New Mock(Of MyAbstractClass)
mockTestObject.CallBase = True
Dim testObject = mockTestObject.Object
Dim castedMock = mockTestObject.As(Of INotifyPropertyChanged)
castedMock.CallBase = True
'
' Act.
'
castedMock.Raise(Sub(c) AddHandler c.PropertyChanged, Nothing,
New PropertyChangedEventArgs("MyProperty1"))
'
' Assert.
'
Assert.AreEqual(100.0R,
testObject.MyProperty1,
"The MyProperty1 value should have been changed.")
End Sub
End Class So this is no longer an Issue per say, more of a bad usage of moq. Sorry about that. |
I am using a mock to verify the core behavior of an abstract class.
Everything is working well until I started to test the code which reacts to the PropertyChanged event of the abstract class.
For some reason when I call .Raise on the mocked object, a new instance of the mock is created and the PropertyChanged event is being called on this new instance which results to crashes since my initial setup is done on the other instance.
`
Dim mockTestObject = New Mock(Of MyAbstractClass)
mockTestObject.CallBase = True
`
If i try to invoke the method which handles the property change directly, all works well (see commented po.Invoke)
See the following call stack:
The text was updated successfully, but these errors were encountered: