Closed GoogleCodeExporter closed 8 years ago
I worked around the issue with the following method:
/// <summary>
/// Returns a component context with the mock as its only registration.
/// </summary>
/// <typeparam name="T">The type of item mocked by the mock.</typeparam>
/// <param name="mock">The mock.</param>
/// <returns>A ComponentContext suitable for use as an Autofac container.</returns>
public static IComponentContext AsComponentContext<T>(this Mock<T> mock) where T : class
{
var builder = new ContainerBuilder();
foreach (var _interface in mock.Object.GetType().GetInterfaces())
{
if (_interface.Namespace == null
|| _interface.Namespace.Contains("Moq")
|| _interface.Namespace.Contains("Castle.DynamicProxy"))
continue;
if (_interface.Name == "ISerializable")
continue;
builder.Register(x => mock.Object).As(_interface);
}
return builder.Build();
}
Original comment by neonta...@gmail.com
on 20 Jul 2011 at 4:52
The AsImplementedInterfaces() method relies on the implementation type of the
component to determine the available interfaces. This works fine for reflective
(RegisterType()-style) components, but in the case of delegates as you've
pointed out, Autofac does not know the implementation type in advance so can't
make the registration correctly. I think the best thing to do here is to make
AsImplementedInterfaces() available only for components with
ReflectionActivatorData. I'll look into making this change in the next release.
Thanks for the information and follow-up!
Nick
Original comment by nicholas...@gmail.com
on 25 Jul 2011 at 10:28
Thank you for letting me know!
Original comment by neonta...@gmail.com
on 3 Aug 2011 at 4:40
I've looked into this - disabling these methods for delegates involve quite a
refactor so I won't take any action on it now. If others hit the same issue I
will revisit it.
Thanks again,
Nick
Original comment by nicholas...@gmail.com
on 12 Aug 2011 at 3:19
Original issue reported on code.google.com by
neonta...@gmail.com
on 20 Jul 2011 at 4:49Attachments: