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

IProtectedMock fails on a new virtual methods #1341

Open
VladimirKhvostov opened this issue May 9, 2023 · 0 comments
Open

IProtectedMock fails on a new virtual methods #1341

VladimirKhvostov opened this issue May 9, 2023 · 0 comments

Comments

@VladimirKhvostov
Copy link

This regression was introduced in v4.7.0 (tested with 4.5.30 - no issues).

Consider the following example:

namespace LibraryTests
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using Moq.Protected;

    public class DataReader
    {
        protected virtual List<string> ExecuteReader() => throw new NotImplementedException();
    }

    public class AdvancedReader : DataReader
    {
        protected new virtual IList<string> ExecuteReader() => base.ExecuteReader();
    }

    [TestClass]
    public class ProtectedTests
    {
        [TestMethod]
        public void Test1()
        {
            Mock<AdvancedReader> mock = new Mock<AdvancedReader>();
            mock.Protected().Setup<IList<string>>("ExecuteReader").Returns(new List<string>() { "1", "2", "3" });
        }
    }
}

Setup fails with the following error:
System.InvalidOperationException: 'Sequence contains more than one matching element'
Stack:

System.Core.dll!System.Linq.Enumerable.SingleOrDefault<System.Reflection.MethodInfo>(System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo> source, System.Func<System.Reflection.MethodInfo, bool> predicate)	Unknown
Moq.dll!Moq.Protected.ProtectedMock<LibraryTests.AdvancedReader>.InternalSetup<System.Collections.Generic.IList<string>>(string methodName, System.Type[] genericTypeArguments, bool exactParameterMatch, object[] args)	Unknown
Moq.dll!Moq.Protected.ProtectedMock<LibraryTests.AdvancedReader>.Setup<System.Collections.Generic.IList<string>>(string methodName, object[] args)	Unknown

Since ExecuteReader in the AdvancedReader class is a new virtual method, I believe reflection returned 2 MethodInfo objects and Moq expects one or zero.

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

1 participant