public void executing_message_handler_without_registering_should_throw_an_exception()
        {
            var executor = new MessageHandlerExecutor(new PoorMansDependencyInjection());

            Func <Task> action = () => executor.Execute(new AppleCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            action.ShouldThrow <TypeAccessException>();
        }
示例#2
0
 public MessageHandlerAction(Type?handlerType)
 {
     HandlerType          = handlerType ?? throw new ArgumentNullException(nameof(handlerType));
     HandlerParameterType = handlerType.GetGenericInterfaceParameters(typeof(IMessageHandler <>))?.Single()
                            ?? throw new ArgumentException("Cannot find generic parameter type.");
     Executor       = new MessageHandlerExecutor(HandlerType, HandlerParameterType);
     _objectFactory = ActivatorUtilities.CreateFactory(handlerType, Type.EmptyTypes);
     Attributes     = handlerType.GetCustomAttributes(false).Cast <Attribute>().AsEnumerable();
 }
        public void executing_message_handler_without_registering_should_throw_an_exception()
        {
            var serviceProvider = new ServiceCollection()
                                  .BuildServiceProvider();
            var executor = new MessageHandlerExecutor(new MicrosoftDependencyInjectionDependencyResolver(serviceProvider));

            Func <Task> action = () => executor.Execute(new AppleCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            action.ShouldThrow <TypeAccessException>();
        }
        public void register_message_handler_as_an_indirect_implementation_should_resolve_and_execute()
        {
            var dependencyInjection = new PoorMansDependencyInjection();

            dependencyInjection.AddMessageHandler(() => new BananaCommandHandler());
            var executor = new MessageHandlerExecutor(dependencyInjection);

            var task = executor.Execute(new BananaCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            task.IsCompleted.Should().BeTrue();
        }
        public void register_message_handler_as_an_indirect_implementation_should_resolve_and_execute()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddScoped <IMessageHandler <BananaCommand>, BananaCommandHandler>()
                                  .BuildServiceProvider();
            var executor = new MessageHandlerExecutor(new MicrosoftDependencyInjectionDependencyResolver(serviceProvider));

            var task = executor.Execute(new BananaCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            task.IsCompleted.Should().BeTrue();
        }
示例#6
0
        void given(IMessageHandler <AppleCommand> handler)
        {
            Func <MessageHandlingExceptionRaisedEventArgs, Task> exceptionHandler = a =>
            {
                exception = a.Exception;
                return(Task.CompletedTask);
            };

            var dependencyResolver = Substitute.For <IDependencyResolver>();

            dependencyResolver.CreateScope().Resolve(Arg.Any <Type>()).ReturnsForAnyArgs(handler);
            executor = new MessageHandlerExecutor(dependencyResolver, exceptionHandler);
        }
        public void thrown_exception_in_message_handler_should_call_exception_handler_and_throw()
        {
            Exception exception = null;
            Func <MessageHandlingExceptionRaisedEventArgs, Task> exceptionHandler = a =>
            {
                exception = a.Exception;
                return(Task.CompletedTask);
            };
            var dependencyInjection = new PoorMansDependencyInjection();

            dependencyInjection.AddMessageHandler(() => new RottenAppleCommandHandler());
            var executor = new MessageHandlerExecutor(dependencyInjection, exceptionHandler);

            Func <Task> action = () => executor.Execute(new AppleCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            action.ShouldThrow <Exception>();
            exception.Message.Should().Be("Rotten Apple");
        }
        public void thrown_exception_in_message_handler_should_call_exception_handler_and_throw()
        {
            Exception exception = null;
            Func <MessageHandlingExceptionRaisedEventArgs, Task> exceptionHandler = a =>
            {
                exception = a.Exception;
                return(Task.CompletedTask);
            };
            var serviceProvider = new ServiceCollection()
                                  .AddScoped <IMessageHandler <AppleCommand>, RottenAppleCommandHandler>()
                                  .BuildServiceProvider();
            var executor = new MessageHandlerExecutor(new MicrosoftDependencyInjectionDependencyResolver(serviceProvider), exceptionHandler);

            Func <Task> action = () => executor.Execute(new AppleCommand(), Substitute.For <IMessageContext>(), new System.Threading.CancellationToken());

            action.ShouldThrow <Exception>();
            exception.Message.Should().Be("Rotten Apple");
        }
 protected void given_dependency_resolver(IDependencyResolver dependencyResolver) => messageHandlerExecutor = new MessageHandlerExecutor(dependencyResolver);