Skip to content

@tallseth /ProxyTest.cs
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simplify testing of proxy/decorator with generics in c#
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();
}
}
@steveburkett

T delagator is misspelled

@tallseth
Owner

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
Something went wrong with that request. Please try again.