private void SetUpCommandHandler() { user = new User(); user.Register("test"); user.MarkPendingEventsAsHandled(); var userRepository = new Mock <IUserRepository>(); userRepository.Setup(f => f.FindUser(It.IsAny <UserIdentity>())) .Returns(user); var auctionCreateSessionService = new Mock <IAuctionCreateSessionService>(); auctionCreateSessionService.Setup(f => f.GetExistingSession()) .Returns(user.UserIdentity.GetAuctionCreateSession()); auctionCreateSessionService.Setup(f => f.RemoveSession()); var eventBusService = new Mock <EventBusService>(Mock.Of <IEventBus>(), Mock.Of <IAppEventBuilder>()); var handlerDepedencies = new CreateAuctionCommandHandlerDepedencies() { auctionRepository = testDepedencies.AuctionRepository, auctionSchedulerService = new TestAuctionSchedulerService(testDepedencies.TimeTaskClient, testDepedencies.TimetaskServiceSettings), eventBusService = eventBusService.Object, logger = Mock.Of <ILogger <CreateAuctionCommandHandler> >(), auctionCreateSessionService = auctionCreateSessionService.Object, auctionImageRepository = testDepedencies.AuctionImageRepository, categoryBuilder = new CategoryBuilder(testDepedencies.CategoryTreeService), userRepository = userRepository.Object }; testCommandHandler = new TestCreateAuctionCommandHandler(handlerDepedencies); }
private void SetUpCommandHandler() { var services = TestDepedencies.Instance.Value; var userRepository = new Mock <IUserRepository>(); userRepository.Setup(f => f.FindUser(It.IsAny <UserIdentity>())) .Returns(user); session.AddOrReplaceImage(new AuctionImage("img1-1", "img1-2", "img1-3"), 0); services.AuctionImageRepository.Add("img1-1", new AuctionImageRepresentation(new AuctionImageMetadata("jpg"), File.ReadAllBytes("./test_image.jpg"))); var sessionService = services.GetAuctionCreateSessionService(session); var handlerDepedencies = new CreateAuctionCommandHandlerDepedencies() { auctionRepository = services.AuctionRepository, auctionSchedulerService = services.SchedulerService, eventBusService = services.EventBus, logger = Mock.Of <ILogger <CreateAuctionCommandHandler> >(), auctionCreateSessionService = sessionService, auctionImageRepository = services.AuctionImageRepository, categoryBuilder = new CategoryBuilder(services.CategoryTreeService), userRepository = userRepository.Object }; commandHandler = new CreateAuctionCommandHandler(handlerDepedencies); }
public void METHOD() { var services = TestDepedencies.Instance.Value; var user = new User(); user.Register("testUserName"); user.MarkPendingEventsAsHandled(); var product = new Product("test product name", "example description", Condition.New); var sem = new SemaphoreSlim(0, 1); var sem2 = new SemaphoreSlim(0, 1); var correlationId = new CorrelationId("test_correlationId"); var categories = new List <string>() { "Fake category", "Fake subcategory", "Fake subsubcategory 0" }; var userRepository = new Mock <IUserRepository>(); userRepository.Setup(f => f.FindUser(It.IsAny <UserIdentity>())) .Returns(user); var command = new CreateAuctionCommand(20.0m, product, DateTime.UtcNow.AddMinutes(10), DateTime.UtcNow.AddDays(12), categories, Tag.From(new[] { "tag1" }), "test auction name", false); command.SignedInUser = user.UserIdentity; IAppEvent <AuctionCreated> publishedEvent = null; var eventHandler = new Mock <TestAuctionCreatedHandler>( services.AppEventBuilder, services.DbContext, Mock.Of <IRequestStatusService>() ); eventHandler.CallBase = true; eventHandler .Setup(f => f.Consume(It.IsAny <IAppEvent <AuctionCreated> >()) ) .Callback((IAppEvent <AuctionCreated> ev) => { publishedEvent = ev; sem.Release(); }) .CallBase(); services.SetupEventBus(eventHandler.Object); var implProv = new Mock <IImplProvider>(); implProv .Setup(f => f.Get <IAuctionRepository>()) .Returns(services.AuctionRepository); RollbackHandlerRegistry.ImplProvider = implProv.Object; var testRollbackHandler = new Mock <TestCreateAuctionRollbackHandler>(implProv.Object); testRollbackHandler.CallBase = true; testRollbackHandler .Setup(f => f.Rollback(It.IsAny <IAppEvent <Event> >())) .CallBase(); testRollbackHandler.Object.AfterAction = () => { sem2.Release(); }; var session = user.UserIdentity.GetAuctionCreateSession(); command.AuctionCreateSession = session; var handlerDepedencies = new CreateAuctionCommandHandlerDepedencies() { auctionRepository = services.AuctionRepository, auctionSchedulerService = services.SchedulerService, eventBusService = services.EventBus, logger = Mock.Of <ILogger <CreateAuctionCommandHandler> >(), auctionCreateSessionService = services.GetAuctionCreateSessionService(session), auctionImageRepository = services.AuctionImageRepository, categoryBuilder = new CategoryBuilder(services.CategoryTreeService), userRepository = userRepository.Object }; var commandHandler = new TestCreateAuctionCommandHandler(handlerDepedencies, testRollbackHandler.Object); commandHandler.Handle(command, CancellationToken.None); if (!sem.Wait(TimeSpan.FromSeconds(60))) { Assert.Fail(); } ; var createdAuciton = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId); if (!sem2.Wait(TimeSpan.FromSeconds(60))) { Assert.Fail(); } ; var auctionAfterRollback = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId); testRollbackHandler.Verify(f => f.Rollback(It.IsAny <IAppEvent <Event> >()), Times.Once()); createdAuciton.Should() .NotBe(null); auctionAfterRollback.Should() .Be(null); }
public TestCreateAuctionCommandHandler(CreateAuctionCommandHandlerDepedencies depedencies, CreateAuctionRollbackHandler rollbackHandler) : base(depedencies) { _createAuctionRollbackHandler = rollbackHandler; }
public TestCreateAuctionCommandHandler(CreateAuctionCommandHandlerDepedencies depedencies) : base(depedencies) { }