Last active
October 12, 2015 14:17
-
-
Save tallseth/4039017 to your computer and use it in GitHub Desktop.
Simplify testing of proxy/decorator with generics in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProxyTest | |
{ | |
public static void AssertProxyDelegation<T,U>(T proxy, Mock<T> proxiedMock, Expression<Func<T,U>> func, U retVal) where T : class | |
{ | |
proxiedMock.Setup(func).Returns(retVal); | |
Assert.That(func.Compile().Invoke(proxy), Is.SameAs(retVal)); | |
} | |
public static void AssertProxyDelegation<T>(T delegator, Mock<T> delegatedTo, Expression<Action<T>> action) where T : class | |
{ | |
delegatedTo.Setup(action).Verifiable(); | |
action.Compile().Invoke(delegator); | |
delegatedTo.Verify(); | |
} | |
} | |
//example usage: | |
[TestFixture] | |
public class ProxyServiceTests | |
{ | |
[Test] | |
public void CallProxied() | |
{ | |
var proxiedMock = new Mock<Service>(); | |
var proxy = Proxy.GetInstance(proxiedMock.Object); | |
ProxyTest.AssertProxyDelegation(proxy, proxiedMock, s=>s.DoSomething()); | |
} | |
} | |
public abstract class Service | |
{ | |
public abstract void DoSomething(); | |
} | |
public class Proxy : Service | |
{ | |
private readonly Service toProxy_; | |
private Proxy(Service toProxy) | |
{ | |
toProxy_ = toProxy; | |
} | |
public static Service GetInstance(Service toProxy) | |
{ | |
return new Proxy(toProxy); | |
} | |
public override void DoSomething() | |
{ | |
toProxy_.DoSomething(); | |
} | |
} |
Thanks Steve, I finally saw this comment.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
T delagator is misspelled