/// <summary>
        /// Retrieve a registration for an unregistered service, to be used
        /// by the container.
        /// </summary>
        /// <param name="service">The service that was requested.</param>
        /// <param name="registrationAccessor"></param>
        /// <returns>
        /// Registrations for the service.
        /// </returns>
        public IEnumerable <IComponentRegistration> RegistrationsFor
            (Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            var typedService = service as IServiceWithType;

            if (typedService == null ||
                !typedService.ServiceType.IsInterface() ||
                typedService.ServiceType.IsGenericType() &&
                typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ||
                typedService.ServiceType.IsArray ||
                typedService.ServiceType.CanBeCastTo <IStartable>())
            {
                return(Enumerable.Empty <IComponentRegistration>());
            }

            var rb = RegistrationBuilder.ForDelegate <object>((c, p) => _mockFactory.CreateMock(typedService.ServiceType))
                     .As(service)
                     .InstancePerLifetimeScope();

            return(new[] { rb.CreateRegistration() });
        }
예제 #2
0
 private void EnsureMockPresent(IDiscriminator <TToken> discriminator)
 {
     if (!_pack.ContainsKey(discriminator))
     {
         _pack.Add(discriminator, _mockFactory.CreateMock <TMock>());
     }
 }
예제 #3
0
        private void RegisterMock(Type serviceType)
        {
            var mockInstance = _mockFactory.CreateMock(serviceType);

            Container.Register(serviceType, mockInstance);
        }
예제 #4
0
 /// <summary>
 /// Creates a mock with the given parameters.
 /// </summary>
 /// <param name="factory">The <see cref="IMockFactory"/> used to create instances of mocks.</param>
 /// <param name="mocksAssembly">Assembly where compile-time generated mocks exist.</param>
 /// <returns>A mock that implements <see cref="IMocked"/> in addition to the specified <typeparamref name="T"/>.</returns>
 public static T CreateMock <T>(this IMockFactory factory, Assembly mocksAssembly)
 => (T)factory.CreateMock(mocksAssembly, typeof(T), new Type[0], new object[0]);