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

Should only add public interfaces #123

Merged
merged 8 commits into from
Aug 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Source/InterceptorStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ internal class InvokeBase : IInterceptStrategy
{
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
{
if (invocation.Method.DeclaringType == typeof(object) ||
invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase
if (invocation.Method.DeclaringType == typeof(object) || // interface proxy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition makes my brain hurt :(. Not sure how to improve it significantly though :(

ctx.Mock.ImplementedInterfaces.Contains(invocation.Method.DeclaringType) && !invocation.Method.IsEventAttach() && !invocation.Method.IsEventDetach() && ctx.Mock.CallBase || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract
invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase // class proxy
)
{
// Invoke underlying implementation.
Expand Down
3 changes: 2 additions & 1 deletion Source/Mock.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using Moq.Language.Flow;
using Moq.Proxy;
Expand Down Expand Up @@ -111,7 +112,7 @@ public Mock(MockBehavior behavior, params object[] args)
this.Behavior = behavior;
this.Interceptor = new Interceptor(behavior, typeof(T), this);
this.constructorArguments = args;
this.ImplementedInterfaces.AddRange(typeof(T).GetInterfaces());
this.ImplementedInterfaces.AddRange(typeof(T).GetInterfaces().Where(i => (i.IsPublic || i.IsNestedPublic) && !i.IsImport));
this.ImplementedInterfaces.Add(typeof(IMocked<T>));

this.CheckParameters();
Expand Down
48 changes: 48 additions & 0 deletions UnitTests/AsInterfaceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public void VerifiesExpectationOnAddedInterfaceCastedDynamically()
bag.As<IFoo>().Verify(f => f.Execute());
}

[Fact]
public void ShouldBeAbleToCastToImplementedInterface()
{
var fooBar = new Mock<FooBar>();
var obj = fooBar.Object;
Assert.DoesNotThrow(() => fooBar.As<IFoo>());
}

[Fact]
public void ShouldNotThrowIfCallExplicitlyImplementedInterfacesMethodWhenCallBaseIsTrue()
{
var fooBar = new Mock<FooBar>();
fooBar.CallBase = true;
var bag = (IBag)fooBar.Object;
Assert.DoesNotThrow(() => bag.Get("test"));
}

public interface IFoo
{
void Execute();
Expand All @@ -168,5 +185,36 @@ public interface IBag
void Add(string key, object o);
object Get(string key);
}

internal interface IBar
{
void Test();
}

public abstract class FooBar : IFoo, IBag, IBar
{
public abstract void Execute();

public abstract string Execute(string command);

public abstract string Execute(string arg1, string arg2);

public abstract string Execute(string arg1, string arg2, string arg3);

public abstract string Execute(string arg1, string arg2, string arg3, string arg4);

public abstract int Value { get; set; }

void IBag.Add(string key, object o)
{
}

object IBag.Get(string key)
{
return null;
}

public abstract void Test();
}
}
}