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

Callbase not working on latest 4.8 version #557

Closed
rotem925 opened this issue Dec 26, 2017 · 6 comments · Fixed by #558
Closed

Callbase not working on latest 4.8 version #557

rotem925 opened this issue Dec 26, 2017 · 6 comments · Fixed by #558
Labels

Comments

@rotem925
Copy link

Hi,
I've upgraded to 4.8.
When setting CallBase=true on new Mock(), and debugging, it seems that the call base does not get called.

Was there any change that could affect this or is there any bug? I didn't see any breaking changes that affect this in the changelog.

Thanks,
Rotem

@stakx
Copy link
Contributor

stakx commented Dec 26, 2017

Hi @rotem925 and thanks for reporting. Could you please provide a short code example that demonstrates the issue you're having?

@rotem925
Copy link
Author

Hi @stakx
Thank you for the quick response,
Here is a simple example of the usage:

public class Program
    {
        static void Main(string[] args)
        {
            var mock = new Mock<MyClass>().As<IMyClass>();
            mock.CallBase = true;
            Assert.NotNull(mock.Object.DoSomething()); //this assertion does not pass!!
        }
        public interface IMyClass
        {
            object DoSomething();
        }
        public class MyClass:IMyClass
        {
            public object DoSomething()
            {
                return new object();
            }
        }
    }

@stakx stakx removed the needs-repro label Dec 26, 2017
@stakx
Copy link
Contributor

stakx commented Dec 26, 2017

@rotem925 - Thanks a lot for the repro code. It looks like this is a regression introduced by 5837c53:

-if (invocation.Method.DeclaringType == typeof(object) || // interface proxy
-	mock.ImplementsInterface(invocation.Method.DeclaringType) && !invocation.Method.LooksLikeEventAttach() && !invocation.Method.LooksLikeEventDetach() && mock.CallBase && !mock.MockedType.GetTypeInfo().IsInterface || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract
-	invocation.Method.DeclaringType.GetTypeInfo().IsClass && !invocation.Method.IsAbstract && mock.CallBase // class proxy
-	)
+if (mock.CallBase && !invocation.Method.IsAbstract)

That cryptic multi-line condition had to go for a variety of reasons; one being that it was near incomprehensible. Replacing it with something much simpler was admittedly a bit of a gamble, but I ended up doing it because there was no test failure hinting at any functional change.

Sorry about that. It should be easily fixed, though. Expect a fixed release soon.

@rotem925
Copy link
Author

That was fast!
Thank you so much.

@stakx
Copy link
Contributor

stakx commented Jan 8, 2018

@rotem925 - The fix for this is included in Moq 4.8.1 which has just been made available on NuGet.

@rotem925
Copy link
Author

rotem925 commented Jan 9, 2018

@stakx Thanks for the update!
It works great now!

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