public void DefaultViewComponentActivator_ActivatesViewComponentContext_IgnoresNonPublic()
        {
            // Arrange
            var expectedInstance = new VisibilityViewComponent();

            var typeActivator = new Mock <ITypeActivatorCache>();

            typeActivator
            .Setup(ta => ta.CreateInstance <object>(It.IsAny <IServiceProvider>(), It.IsAny <Type>()))
            .Returns(expectedInstance);

            var activator = new DefaultViewComponentActivator(typeActivator.Object);

            var context = CreateContext(typeof(VisibilityViewComponent));

            expectedInstance.ViewComponentContext = context;

            // Act
            var instance = activator.Create(context) as VisibilityViewComponent;

            // Assert
            Assert.NotNull(instance);
            Assert.Same(context, instance.ViewComponentContext);
            Assert.Null(instance.C);
        }
        public void Create_ThrowsIfControllerCannotBeActivated(Type type)
        {
            // Arrange
            var actionDescriptor = new ViewComponentDescriptor
            {
                TypeInfo = type.GetTypeInfo()
            };

            var context = new ViewComponentContext
            {
                ViewComponentDescriptor = actionDescriptor,
                ViewContext             = new ViewContext
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        RequestServices = Mock.Of <IServiceProvider>()
                    },
                }
            };

            var activator = new DefaultViewComponentActivator(new TypeActivatorCache());

            // Act and Assert
            var exception = Assert.Throws <InvalidOperationException>(() => activator.Create(context));

            Assert.Equal(
                $"The type '{type.FullName}' cannot be activated by '{typeof(DefaultViewComponentActivator).FullName}' " +
                "because it is either a value type, an interface, an abstract class or an open generic type.",
                exception.Message);
        }
        public async Task DefaultViewComponentActivator_ReleaseAsync_PrefersAsyncDisposableOverDisposable()
        {
            // Arrange
            var instance = new SyncAndAsyncDisposableViewComponent();

            var activator = new DefaultViewComponentActivator(Mock.Of <ITypeActivatorCache>());

            var context = CreateContext(typeof(SyncAndAsyncDisposableViewComponent));

            // Act
            await activator.ReleaseAsync(context, instance);

            // Assert
            Assert.True(instance.AsyncDisposed);
            Assert.False(instance.SyncDisposed);
        }