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

Verify behavior change using DefaultValue.Mock 4.12.0 vs 4.13.0 #1024

Closed
DesrosiersC opened this issue Jun 15, 2020 · 10 comments · Fixed by #1025 or #1027
Closed

Verify behavior change using DefaultValue.Mock 4.12.0 vs 4.13.0 #1024

DesrosiersC opened this issue Jun 15, 2020 · 10 comments · Fixed by #1025 or #1027
Assignees
Labels
Milestone

Comments

@DesrosiersC
Copy link

Hi,

I have a set of unit tests which start failing after upgrade from Moq 4.12.0 to 4.13.0.
Here's code which illustrates the issue:

using System;
using System.Collections.Generic;
using Moq;

namespace MoqRegression
{
	internal static class Program
	{
		private static void Main( string[] args )
		{
			var totoMock = new Mock<IToto>();
			totoMock.DefaultValue = DefaultValue.Mock;

			totoMock.Setup( o => o.Do() )
				.Verifiable();

			totoMock.Object.Do();

			totoMock.Verify();

			Console.ReadLine();
		}
	}

	public interface IToto
	{
		IList<string> Do();
	}
}

The verify fails from version 4.1.13.0 and above.

Could you please let me know if the change of behavior is a bug and if I should wait for the fix.

Thanks,
Christian

@stakx stakx added the bug label Jun 15, 2020
@stakx
Copy link
Contributor

stakx commented Jun 15, 2020

Thanks for reporting. Looks like a bug. I'll take a look as soon as I find a free moment.

@stakx
Copy link
Contributor

stakx commented Jun 16, 2020

This should be fixed in Moq version 4.14.2 (which has just become available on NuGet). Thanks again for reporting this regression, @DesrosiersC!

@DesrosiersC
Copy link
Author

Hi,

Thanks @stakx for being so responsive!

Moq is a great product and it is good to see it is so well maintained.

@stakx
Copy link
Contributor

stakx commented Jun 16, 2020

Thanks for the kind feedback! ❤️

@DesrosiersC
Copy link
Author

DesrosiersC commented Jun 18, 2020

Hi,

@stakx
The fix is OK for the initial example but I'm still experiencing a breaking change for the example below.

using System;
using System.Collections.Generic;
using Moq;

namespace MoqRegression
{
	internal static class Program
	{
		private static void Main( string[] args )
		{
			var totoMock = new Mock<IToto>();
			var tataMock = new Mock<ITata>();
			tataMock.DefaultValue = DefaultValue.Mock;

			totoMock.Setup( o => o.DoToto() )
				.Returns( tataMock.Object )
				.Verifiable();

			totoMock.Object.DoToto();
			tataMock.Object.DoTata();

			totoMock.Verify();

			Console.WriteLine( "Success!" );
			Console.ReadLine();
		}
	}

	public interface IToto
	{
		ITata DoToto();
	}

	public interface ITata
	{
		IList<string> DoTata();
	}
}

The verify fails from version 4.14.0 and above.

Thanks in advance for taking a look at this,
Christian

@stakx
Copy link
Contributor

stakx commented Jun 18, 2020

@DesrosiersC — Thanks again for the report. I see, I wasn't quite thorough enough.

Note to self, I suspect the problem lies (once again) here:

https://github.com/moq/moq4/blob/a20d1df93b678edc413ef8f0865a70c79d15dae9/src/Moq/Interception/InterceptionAspects.cs#L264-L268

Could be that the setup being added (the one for the auto-mocked return value) doesn't get marked as matched right away.

@stakx stakx reopened this Jun 18, 2020
@stakx stakx self-assigned this Jun 18, 2020
@connywesth
Copy link

connywesth commented Jun 18, 2020

I'm pretty newbie at Moq at all, so I may not have understood the problem, but this code I got to work:

`using System;
using System.Collections.Generic;
using Moq;

namespace MoqRegression
{
internal static class Program
{
private static void Main(string[] args)
{
Mock totoMock = new Mock();
Mock tataMock = new Mock();
Mock tataListMock = new Mock();

		totoMock.DefaultValue = DefaultValue.Mock;
		tataMock.DefaultValue = DefaultValue.Mock;
		tataListMock.DefaultValue = DefaultValue.Mock;

		totoMock.Setup(o => o.DoToto())
			.Returns(tataMock.Object)
			.Verifiable();

		tataMock.Setup(o => o.DoTataList())
			.Returns(tataListMock.Object)
			.Verifiable();

		totoMock.Object.DoToto();
		tataMock.Object.DoTataList();

		totoMock.Verify();
		tataMock.Verify();

		Console.WriteLine("Success!");
		Console.ReadLine();
	}
}

public interface IToto
{
	ITata DoToto();
}

public interface ITata
{
	ITataList DoTataList();
}
public interface ITataList : IList<string>
{
}

}`

@DesrosiersC
Copy link
Author

Thanks for your help @connywesth .
The problem is that I do not wish to declare nor verify the second mock. Also I have lots of tests failing because of this behaviour change and it would be quite a job to change them all.

@stakx
Copy link
Contributor

stakx commented Jun 18, 2020

@DesrosiersC, another fixed package version 4.14.3 should become available on NuGet shortly. Hopefully, this time it's fixed for real. 😃 If you find anything else amiss, please don't hesitate to report it.

@DesrosiersC
Copy link
Author

@stakx , thanks for taking the time to look at this. It is much appreciated.
It works perfectly now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment