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

RegisterDecorator not functioning as expected #21

Closed
Thorarin opened this issue Nov 14, 2018 · 1 comment
Closed

RegisterDecorator not functioning as expected #21

Thorarin opened this issue Nov 14, 2018 · 1 comment

Comments

@Thorarin
Copy link

When using AutoMock, it's possible to build the underlying container that is used to resolve dependencies. This is useful if a test requires a few specific dependencies are fulfilled by the actual implementations, for example.

I've run into a problem when using this pattern: registered decorators seem to be ignored.
Instead of my registered decorator getting injected, a new new mock is automatically created and used instead.

In code, this does not work as expected:

var autoMock = AutoMock.GetLoose(builder =>
{
    builder.RegisterType<Service>().Named<IService>("uncached");
    builder.RegisterDecorator<IService>(
        (ctx, inner) => new CachingService(inner), "uncached");
});

By comparison, this workaround does the job:

var autoMock = AutoMock.GetLoose(builder =>
{
    builder.RegisterType<Service>().Named<IService>("uncached");
    builder.RegisterType<CachingService>()
        .WithParameter(new ResolvedParameter(
            (pi, ctx) => pi.ParameterType == typeof(IService),
            (pi, ctx) => ctx.ResolveNamed<IService>("uncached")))
        .As<IService>();
});

I may take a look myself as to the cause of this, but I can't make any promises 😄

Used versions

<package id="Autofac" version="4.8.1" targetFramework="net471" />
<package id="Autofac.Extras.Moq" version="4.3.0" targetFramework="net471" />
<package id="Moq" version="4.10.0" targetFramework="net471" />

Reproduction
I've added a test that demonstrates the problem in this fork:
https://github.com/Thorarin/Autofac.Extras.Moq/tree/feat/decorators

@alistairjevans
Copy link
Member

As of the imminent version 5.0 release of this integration, this issue has been resolved.

@tillig tillig closed this as completed Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants