Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:07:05+00:00 2026-05-14T01:07:05+00:00

I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)

  • 0

I have a constructor which has a non-interface dependency:

public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)

I am using the Moq.Contrib automockcontainer. If I try to automock the MainWindowViewModel class, I get an error due to the WeekNavigatorViewModel dependency.

Are there any automocking containers which supports mocking of non-interface types?

As Mark has shown below; yes you can! 🙂 I replaced the Moq.Contrib AutoMockContainer with the stuff Mark presents in his answer, the only difference is that the auto-generated mocks are registered as singletons, but you can make this configurable. Here is the final solution:

/// <summary>
/// Auto-mocking factory that can create an instance of the 
/// class under test and automatically inject mocks for all its dependencies.
/// </summary>
/// <remarks>
/// Mocks interface and class dependencies
/// </remarks>
public class AutoMockContainer
{
    readonly IContainer _container;

    public AutoMockContainer(MockFactory factory)
    {
        var builder = new ContainerBuilder();

        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
        builder.RegisterSource(new MoqRegistrationSource(factory));

        _container = builder.Build();
    }

    /// <summary>
    /// Gets or creates a mock for the given type, with 
    /// the default behavior specified by the factory.
    /// </summary>
    public Mock<T> GetMock<T>() where T : class
    {
        return (_container.Resolve<T>() as IMocked<T>).Mock;
    }

    /// <summary>
    /// Creates an instance of a class under test, 
    /// injecting all necessary dependencies as mocks.
    /// </summary>
    /// <typeparam name="T">Requested object type.</typeparam>
    public T Create<T>() where T : class
    {
        return _container.Resolve<T>();
    }

    public T Resolve<T>()
    {
        return _container.Resolve<T>();
    }

    /// <summary>
    /// Registers and resolves the given service on the container.
    /// </summary>
    /// <typeparam name="TService">Service</typeparam>
    /// <typeparam name="TImplementation">The implementation of the service.</typeparam>
    public void Register<TService, TImplementation>()
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<TImplementation>().As<TService>().SingleInstance();
        builder.Update(_container);
    }

    /// <summary>
    /// Registers the given service instance on the container.
    /// </summary>
    /// <typeparam name="TService">Service type.</typeparam>
    /// <param name="instance">Service instance.</param>
    public void Register<TService>(TService instance)
    {
        var builder = new ContainerBuilder();

        if (instance.GetType().IsClass)
            builder.RegisterInstance(instance as object).As<TService>();
        else
            builder.Register(c => instance).As<TService>();

        builder.Update(_container);
    }

    class MoqRegistrationSource : IRegistrationSource
    {
        private readonly MockFactory _factory;
        private readonly MethodInfo _createMethod;

        public MoqRegistrationSource(MockFactory factory)
        {
            _factory = factory;
            _createMethod = factory.GetType().GetMethod("Create", new Type[] { });
        }

        public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            var swt = service as IServiceWithType;
            if (swt == null)
            {
                yield break;
            }

            if (!swt.ServiceType.IsInterface)
                yield break;

            var existingReg = registrationAccessor(service);
            if (existingReg.Any())
            {
                yield break;
            }

            var reg = RegistrationBuilder.ForDelegate((c, p) =>
            {
                var createMethod = _createMethod.MakeGenericMethod(swt.ServiceType);
                return ((Mock)createMethod.Invoke(_factory, null)).Object;
            }).As(swt.ServiceType).SingleInstance().CreateRegistration();

            yield return reg;
        }

        public bool IsAdapterForIndividualComponents
        {
            get { return false; }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:07:05+00:00Added an answer on May 14, 2026 at 1:07 am

    You can pretty easily write one yourself if you leverage a DI Container that supports just-in-time resolution of requested types.

    I recently wrote a prototype for exactly that purpose using Autofac and Moq, but other containers could be used instead.

    Here’s the appropriate IRegistrationSource:

    public class AutoMockingRegistrationSource : IRegistrationSource
    {
        private readonly MockFactory mockFactory;
    
        public AutoMockingRegistrationSource()
        {
            this.mockFactory = new MockFactory(MockBehavior.Default);
            this.mockFactory.CallBase = true;
            this.mockFactory.DefaultValue = DefaultValue.Mock;
        }
    
        public MockFactory MockFactory
        {
            get { return this.mockFactory; }
        }
    
        #region IRegistrationSource Members
    
        public IEnumerable<IComponentRegistration> RegistrationsFor(
            Service service,
            Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            var swt = service as IServiceWithType;
            if (swt == null)
            {
                yield break;
            }
    
            var existingReg = registrationAccessor(service);
            if (existingReg.Any())
            {
                yield break;
            }
    
            var reg = RegistrationBuilder.ForDelegate((c, p) =>
                {
                    var createMethod = 
                        typeof(MockFactory).GetMethod("Create", Type.EmptyTypes).MakeGenericMethod(swt.ServiceType);
                    return ((Mock)createMethod.Invoke(this.MockFactory, null)).Object;
                }).As(swt.ServiceType).CreateRegistration();
    
            yield return reg;
        }
    
        #endregion
    }
    

    You can now set up the container in a unit test like this:

    [TestMethod]
    public void ContainerCanCreate()
    {
        // Fixture setup
        var builder = new ContainerBuilder();
        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
        builder.RegisterSource(new AutoMockingRegistrationSource());
        var container = builder.Build();
        // Exercise system
        var result = container.Resolve<MyClass>();
        // Verify outcome
        Assert.IsNotNull(result);
        // Teardown
    }
    

    That’s all you need to get started.

    MyClass is a concrete class with an abstract dependency. Here is the constructor signature:

    public MyClass(ISomeInterface some)
    

    Notice that you don’t have to use Autofac (or any other DI Container) in your production code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 409k
  • Answers 409k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer of the top of my head, albeit there is surely… May 15, 2026 at 7:09 am
  • Editorial Team
    Editorial Team added an answer To do that, each button will need to send a… May 15, 2026 at 7:09 am
  • Editorial Team
    Editorial Team added an answer I would use a regex, especially if it's possible there's… May 15, 2026 at 7:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.