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

VerifySet breaks with System.NullReferenceException for WriteOnly-Indexed-Properties #1036

Closed
Epicycle23 opened this issue Jun 30, 2020 · 5 comments · Fixed by #1037
Closed
Assignees
Labels
Milestone

Comments

@Epicycle23
Copy link

Epicycle23 commented Jun 30, 2020

Version: 4.14.4
Language: Visual Basic
Environment: Visual Studio 2019 16.6.2

Steps to reproduce

<TestClass()>
Public Class UnitTest1


    Public Interface ITest
        WriteOnly Property Value(key As String)
    End Interface

    <TestMethod()>
    Public Sub TestMethod1()

        Dim mock As New Mock(Of ITest)
        mock.Object.Value("key") = "value"
        mock.VerifySet(Sub(x) x.Value("key") = It.IsAny(Of String), Times.Once)

    End Sub

End Class

Was working fine with version 4.8.3.
I ran into this issue when trying to update to 4.14.4

@stakx
Copy link
Contributor

stakx commented Jun 30, 2020

Hi @Epicycle23 , thanks for reporting. I'll take a look a little later.

@stakx
Copy link
Contributor

stakx commented Jun 30, 2020

OK, this broke between versions 4.10.1 and 4.11.0, and the problem lies in this line of code:

https://github.com/moq/moq4/blob/be325ce3971d7d4efa432f8ebcbe10763f027e91/src/Moq/ExpressionExtensions.cs#L262

This is supposed to extract the method from the expression tree, and obviously always choosing the getter won't do any good for write-only indexers.

For regular properties, we have a different code path that accounts for the write-only special case:

https://github.com/moq/moq4/blob/be325ce3971d7d4efa432f8ebcbe10763f027e91/src/Moq/ExpressionExtensions.cs#L290-L297

However, splitting assignment expressions and both code paths mentioned above need some improving, they're rather hard to understand. I'll see what I can come up with.

@stakx
Copy link
Contributor

stakx commented Jun 30, 2020

OK, this should be fixed. The expression tree logic I had to change is somewhat difficult to understand, so I might do some refactoring before releasing a bugfix version on NuGet. It should happen sometime during the next few days.

Thanks again for reporting this regression!

@Epicycle23
Copy link
Author

Thanks for the fast response/fix, can't wait to update.

@stakx
Copy link
Contributor

stakx commented Jul 1, 2020

can't wait to update

Update: Version 4.14.5 should become available on NuGet shortly.


If you're in a hurry and you cannot wait, you can use the CI NuGet feed at https://ci.appveyor.com/nuget/moq4, or download the NuGet packages generated on the CI server from https://ci.appveyor.com/project/MobileEssentials/moq4/build/artifacts.

(Mind that the package version of the CI packages isn't right, so if you do use those packages, you'll need to flush them from your NuGet package cache before switching to the official package.)

I'll likely do a release today or tomorrow.

@stakx stakx added this to the 4.14.5 milestone Jul 1, 2020
@stakx stakx added the bug label Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants