Bug in DynamicProxy for templated types?

5 views
Skip to first unread message

Nigel

unread,
Aug 4, 2008, 6:30:32 PM8/4/08
to LinFu.Framework
Hi Guys.. I can't get google to let me add an issue.

Here's the Problem

//--------

public class PassThrough<T> : IInterceptor
{
private readonly T target;

public PassThrough(T target)
{
this.target = target;
}

object IInterceptor.Intercept(InvocationInfo info)
{
try
{
return info.TargetMethod.Invoke(target,
info.Arguments);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
}

[Test]
public void CannotProxyTemplatesTypes()
{
X<IA> mockX = NewMock<X<IA>>();
PassThrough<X<IA>> passthrough = new
PassThrough<X<IA>>(mockX);
ProxyFactory factory = new ProxyFactory();
X<IA> proxy = factory.CreateProxy<X<IA>>(passthrough);

IA ia = NewMock<IA>();

Expect.Once.On(mockX).Method("DoSomething").WithNoArguments().Will(Return.Value(ia));

Assert.AreSame(ia, proxy.DoSomething());
}
//--------

Philip_L

unread,
Aug 4, 2008, 7:23:10 PM8/4/08
to LinFu.Framework
Hi Nigel,

What does class X<T> look like? Is there any way you can post the
whole code listing?

Nigel

unread,
Aug 4, 2008, 7:43:49 PM8/4/08
to LinFu.Framework
I've done some more looking...
It looks like it's related to using NMock with LinFu.

using

class XIA : X<IA>
{
private IA result;

public XIA(IA result)
{
this.result = result;
}

public IA DoSomething()
{
return result;
}
}

instead of a mock object .. like this...

[Test]
public void CanProxyGenericTypes()
{
IA ia = NewMock<IA>();
X<IA> mockX = new XIA(ia);

PassThrough<X<IA>> passthrough = new
PassThrough<X<IA>>(mockX);
ProxyFactory factory = new ProxyFactory();
X<IA> proxy = factory.CreateProxy<X<IA>>(passthrough);

Assert.AreSame(ia, proxy.DoSomething());
}

passes.

Nigel

unread,
Aug 4, 2008, 7:50:15 PM8/4/08
to LinFu.Framework
Hi Philip

It looks like it is a problem using NMock with Dynamic Proxy..

I replaced a mock object with a real object and the test passed.

so... using new XIA instead of NewMock<X<IA>>(); makes it work.
Using NMock causes a Bad IL exception.

class XIA : X<IA>
{
private IA result;

public XIA(IA result)
{
this.result = result;
}

public IA DoSomething()
{
return result;
}
}

public interface X<T>
{
T DoSomething();
}

public interface IA
{

}


my test now reads...

[TestFixture]
public class ProxyFactoryTests : MockingTestFixture
{

[Test]
public void CanProxyGenericTypes()
{
IA ia = NewMock<IA>();
X<IA> mockX = new XIA(ia);

PassThrough<X<IA>> passthrough = new
PassThrough<X<IA>>(mockX);
ProxyFactory factory = new ProxyFactory();
X<IA> proxy = factory.CreateProxy<X<IA>>(passthrough);

Assert.AreSame(ia, proxy.DoSomething());
}

protected override void SetUp()
{

}
}

Note: MockingTestFixture is just a baseclass for mocking tests that
creates a mockery, exposed NewMock as a helper method and does
validation on teardown. (From NMockExtensions)

Philip_L

unread,
Aug 4, 2008, 8:00:09 PM8/4/08
to LinFu.Framework
That's interesting--I always kept running into type identity issues
when using LinFu with NMock,. At least you got it working. As for
mocks, I've recently switched LinFu v2.0 to Moq and it's been a
pleasure to use. Anyway, I'm glad you got it fixed.

Regards,

Philip Laureano
Reply all
Reply to author
Forward
0 new messages