Use Nsubstitute to register or configure for an IOC container

I have a custom IOC container that accepts an interface and type Concrete as a parameter for registration. In my project, I registered the configuration as indicated in the code below. Can you help me register with a unit testing project using NSubstitute?

IOC -Conatincer.cs

Register<Intf, Impl>();

Application - Configuration.cs

Register<ICustomer,Customer>();

Unit Test Application - CustomerTest.cs

Register<ICustomer,StubCustomer>(); -I want something like this
var substitute = Substitute.For<ICustomer>(); but It provides something like this
+4
source share
2 answers

, , Unity, / NSubstitute.

, , NSubstitue, / , .

0

Concrete. OverLoaded Register()

Container.cs

 public class IOCContainer
 {
    static Dictionary<Type, Func<object>> registrations = new Dictionary<Type, Func<object>>();
    public static void Register<TService, TImpl>() where TImpl : TService
    {
        registrations.Add(typeof(TService), () => Resolve(typeof(TImpl)));
    }
    public static void Register<TService>(TService instance)
    {
        registrations.Add(typeof(TService), () => instance);
    }
    public static TService Resolve<TService>()
    {
        return (TService)Resolve(typeof(TService));
    }
    private static object Resolve(Type serviceType)
    {
        Func<object> creator;
        if (registrations.TryGetValue(serviceType, out creator)) return creator();
        if (!serviceType.IsAbstract) return CreateInstance(serviceType);
        else throw new InvalidOperationException("No registration for " + serviceType);
    }
    private static object CreateInstance(Type implementationType)
    {
        var ctor = implementationType.GetConstructors().Single();
        var parameterTypes = ctor.GetParameters().Select(p => p.ParameterType).ToList();
        var dependencies = parameterTypes.Select(Resolve).ToArray();            
        return Activator.CreateInstance(implementationType, dependencies);
    }
}

Configuration.cs

IOCContainer.Register(Substitute.For<IProvider>());
IOCContainer.Register(Substitute.For<ICustomer>());
0

All Articles