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

Q: Verify with It.IsAny for structs? #601

Closed
Muhomorik opened this issue Mar 6, 2018 · 3 comments
Closed

Q: Verify with It.IsAny for structs? #601

Muhomorik opened this issue Mar 6, 2018 · 3 comments

Comments

@Muhomorik
Copy link

Hello,

I have a strange problem when I try to verify with It.IsAny on Struct in Loose mode.

Struct is from another library and I have no control over it. It is passed as out argument of a method.

I can verify the specific calls with specific values and it is correct, but when I use It.IsAny it always says 0 calls.

I can see that the Invocations are set and there is only one invocation with values that were actually set.
Is it some kind of limitation for structs?

Trace:

Moq.MockException : Must change next item.
Expected invocation on the mock once, but was 0 times: x => x.ItemSet(It.IsAny<DATETIME_STRUCT>(), True)

Configured setups: 
x => x.ItemSet(It.IsAny<DATETIME_STRUCT>(), True)

Performed invocations: 
MyItem.ItemSet(DATETIME_STRUCT, True)
   vid Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount) i C:\projects\moq4\Source\Mock.cs:rad 473
   vid Moq.Mock.VerifyCalls(Mock targetMock, MethodCall expected, Expression expression, Times times) i C:\projects\moq4\Source\Mock.cs:rad 451
   vid Moq.Mock.Verify[T](Mock`1 mock, Expression`1 expression, Times times, String failMessage) i C:\projects\moq4\Source\Mock.cs:rad 312
   vid Moq.Mock`1.Verify(Expression`1 expression, Times times, String failMessage) i C:\projects\moq4\Source\Mock.Generic.cs:rad 479
@stakx
Copy link
Contributor

stakx commented Mar 6, 2018

Moq should support structs just fine. There's probably something wrong with your code. Could you please provide a small code example demonstrating the problem? Otherwise it's hard to say what exactly is going on.

@Muhomorik
Copy link
Author

Hi,
Here is a repo (sorry for VB :) )

https://www.dropbox.com/s/uiq2gor85rb57r1/NUnitAndMoq.Tests.zip?dl=0

/ Best regards

@stakx stakx removed the needs-repro label Mar 9, 2018
@stakx
Copy link
Contributor

stakx commented Mar 9, 2018

_childMock.Verify(Sub(x) x.GetStructByRef(It.IsAny(Of StructOne)), Times.Once())

The method that you're setting up takes its argument by reference. It.IsAny(Of T) does not work as expected with by-ref parameters (and unlike in VB.NET, in C# you'd even get a compilation error). If you want to match any value for a by-ref parameter, use It.ByRef(Of StructOne).IsAny instead:

_childMock.Verify(Sub(x) x.GetStructByRef(It.Ref(Of StructOne).IsAny), Times.Once())

@stakx stakx closed this as completed Mar 9, 2018
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