'Moq, mockin an abstract class : the mocked object does cannot use methods implemented in the abstract class

hi I have a code similar to the following:

        public abstract class MyAbstractClass
        {
            public virtual string DoSomething(string s1, string s2)
            {
                return this.DoSomethingElse(s1 + s2);
            }

            protected abstract string DoSomethingElse(string s);
        }


        [TestMethod]
        public void Test()
        {
            var myMock = new Mock<MyAbstractClass>();
            myMock.Protected().Setup<string>("DoSomethingElse", ItExpr.IsAny<string>()).Returns<string>(x => $"{x} I did it.");

            var result = myMock.Object.DoSomething("a", "b");

        }

I would expect the result in the test to be "ab I did it", but I get back a null. I noticed that if remove the "virtual" to the "DoSomething" method, than it would return as expected. The problem is that I need this method to be virtual. And honestly in my understanding of how moq work it should return the "ab I did it", but maybe I am missing something.

Any idea? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source