public async Task Success() { // Arrange var mockServiceBusPubliser = new Mock <IServiceBusPublisher>(); mockServiceBusPubliser.Setup(serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>())) .Verifiable(); var user = new User { Id = Guid.NewGuid(), Email = "*****@*****.**", EmailConfirmed = true, UserName = "******", Country = Country.Canada }; var account = new Account(user.Id.ConvertTo <UserId>()); var moneyAccount = new MoneyAccountDecorator(account); moneyAccount.Deposit(Money.TwoHundred).MarkAsSucceeded(); var doxatag = new Doxatag( account.Id, "TestUser", 1000, new UtcNowDateTimeProvider()); var gamePlayerId = new PlayerId(); var challenge = new Challenge( new ChallengeId(), new ChallengeName("TestChallenge"), Game.LeagueOfLegends, BestOf.One, Entries.Two, new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.OneDay), Scoring); var payout = new ChallengePayoutFactory(); var challengePayout = new Cashier.Domain.AggregateModels.ChallengeAggregate.Challenge( challenge.Id, payout.CreateInstance().GetChallengePayout(ChallengePayoutEntries.One, MoneyEntryFee.OneHundred)); using var gamesHost = new GamesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id)); gamesHost.Server.CleanupDbContext(); await gamesHost.Server.UsingScopeAsync( async scope => { var gameCredentialRepository = scope.GetRequiredService <IGameCredentialRepository>(); gameCredentialRepository.CreateCredential( new Credential( account.Id, challenge.Game, gamePlayerId, new UtcNowDateTimeProvider())); await gameCredentialRepository.UnitOfWork.CommitAsync(false); }); var gameServiceClient = new GameService.GameServiceClient(gamesHost.CreateChannel()); using var challengesHost = new ChallengesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id)) .WithWebHostBuilder( builder => { builder.ConfigureTestContainer <ContainerBuilder>( container => { container.RegisterInstance(mockServiceBusPubliser.Object).As <IServiceBusPublisher>().SingleInstance(); }); }); challengesHost.Server.CleanupDbContext(); await challengesHost.Server.UsingScopeAsync( async scope => { var challengeRepository = scope.GetRequiredService <IChallengeRepository>(); challengeRepository.Create(challenge); await challengeRepository.CommitAsync(false); }); var challengeServiceClient = new ChallengeService.ChallengeServiceClient(challengesHost.CreateChannel()); using var cashierHost = new CashierHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id)); cashierHost.Server.CleanupDbContext(); await cashierHost.Server.UsingScopeAsync( async scope => { var accountRepository = scope.GetRequiredService <IAccountRepository>(); accountRepository.Create(account); await accountRepository.CommitAsync(); }); await cashierHost.Server.UsingScopeAsync( async scope => { var challengeRepository = scope.GetRequiredService <IChallengePayoutRepository>(); challengeRepository.Create(challengePayout); await challengeRepository.CommitAsync(); }); var cashierServiceClient = new CashierService.CashierServiceClient(cashierHost.CreateChannel()); using var identityHost = new IdentityHostFactory(); identityHost.Server.CleanupDbContext(); await identityHost.Server.UsingScopeAsync( async scope => { var doxatagRepository = scope.GetRequiredService <IUserService>(); await doxatagRepository.CreateAsync(user, "Pass@word1"); }); await identityHost.Server.UsingScopeAsync( async scope => { var doxatagRepository = scope.GetRequiredService <IDoxatagRepository>(); doxatagRepository.Create(doxatag); await doxatagRepository.UnitOfWork.CommitAsync(false); }); var identityServiceClient = new IdentityService.IdentityServiceClient(identityHost.CreateChannel()); using var challengesAggregatorHost = new ChallengesWebAggregatorHostFactory() .WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id)) .WithWebHostBuilder( builder => { builder.ConfigureTestContainer <ContainerBuilder>( container => { container.RegisterInstance(challengeServiceClient).SingleInstance(); container.RegisterInstance(cashierServiceClient).SingleInstance(); container.RegisterInstance(identityServiceClient).SingleInstance(); container.RegisterInstance(gameServiceClient).SingleInstance(); }); }); var client = challengesAggregatorHost.CreateClient(); // Act var response = await client.PostAsJsonAsync( $"api/challenges/{challenge.Id}/participants", new { }); // Assert response.EnsureSuccessStatusCode(); mockServiceBusPubliser.Verify( serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>()), Times.Once); }