Skip to content
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

Closed
Sl1ckR1ck opened this issue Dec 1, 2021 · 2 comments
Closed

Comments

@Sl1ckR1ck
Copy link

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

            ' Setup some mustoverride properties.
            mockTestObject.SetupProperty(Function(c) c.Property1)
            mockTestObject.SetupProperty(Function(c) c.Property2)
            mockTestObject.SetupProperty(Function(c) c.Property3)

            Dim testObject = mockTestObject.Object

            Dim po = New PrivateObject(testObject , New PrivateType(GetType(MyAbstractClass))
            '
            ' Act.
            '
            ' po.Invoke("OnPropertyChanged", {TestObject, New PropertyChangedEventArgs(propertyName)})
            m_mockTestObject.Raise(Sub(c) AddHandler c.PropertyChanged, Nothing,
                                   New PropertyChangedEventArgs(propertyName))

`

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:
moq raise issue

@stakx
Copy link
Contributor

stakx commented Dec 2, 2021

Please post minimal but complete repro code. You've excluded the required bits of the definition of MyAbstractClass so it's impossible to run your code as is.

@Sl1ckR1ck
Copy link
Author

Sl1ckR1ck commented Dec 2, 2021

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.

@stakx stakx removed the needs-repro label Dec 5, 2021
@stakx stakx closed this as completed Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants