Example #1
0
            public async Task ShouldSucceed()
            {
                var services = new ServiceCollection();

                _ = services.AddScoped <ILoggerFactory>(_ => this.Log);
                _ = services.AddLogging();

                var databaseName = Guid.NewGuid().ToString();

                _ = services.AddTransient(_ => new DbContextOptionsBuilder <FakeDbContext>()
                                          .UseInMemoryDatabase(databaseName: databaseName)
                                          .Options);

                MediatrHelpers.RegisterMediatrWithExplicitTypes(
                    services,
                    null,
                    new MediatRServiceConfiguration(),
                    new FakeMediatrRegistration());

                var serviceProvider = services.BuildServiceProvider();

                var dbContextOptions = serviceProvider.GetService <DbContextOptions <FakeDbContext> >();

                using (var dbContext = new FakeDbContext(dbContextOptions !))
                {
                    var entityCount = dbContext.FakeAddAudit.Count();
                    Assert.Equal(0, entityCount);

                    entityCount = dbContext.FakeAddPreProcessAudit.Count();
                    Assert.Equal(0, entityCount);

                    entityCount = dbContext.FakeAddPostProcessAudit.Count();
                    Assert.Equal(0, entityCount);
                }

                var       mediator   = serviceProvider.GetService <IMediator>();
                const int expected   = 987654321;
                var       request    = new FakeCrudAddCommand(expected, ClaimsPrincipal.Current !);
                var       sendResult = await mediator !.Send(request).ConfigureAwait(false);

                Assert.Equal(expected, sendResult);

                using (var dbContext = new FakeDbContext(dbContextOptions !))
                {
                    var entityCount = dbContext.FakeAddAudit.Count();
                    Assert.Equal(1, entityCount);

                    entityCount = dbContext.FakeAddPreProcessAudit.Count();
                    Assert.Equal(1, entityCount);

                    entityCount = dbContext.FakeAddPostProcessAudit.Count();
                    Assert.Equal(1, entityCount);
                }

                var notification = new FakeNotification();
                await mediator.Publish(notification)
                .ConfigureAwait(false);
            }
Example #2
0
        private void ConfigureMediatrService(IServiceCollection services)
        {
            var mediatrRegistration = GetMediatrRegistration();

            MediatrHelpers.RegisterMediatrWithExplicitTypes(
                services,
                null,
                new MediatRServiceConfiguration(),
                mediatrRegistration);
        }