public void AddContactWithNullValuesThrowsException()
        {
            var cmd     = new AddContactCommand(null);
            var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object);

            Assert.Throws <InvalidEntityException>(() => handler.Handle(cmd, new System.Threading.CancellationToken()));
        }
        public void AddContactWithInvalidValuesThrowsException()
        {
            var model = ContactsModelObjectMother.Random();

            model.FirstName = null;
            var cmd     = new AddContactCommand(model);
            var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object);

            Assert.Throws <EntityValidationException>(() => handler.Handle(cmd, new System.Threading.CancellationToken()));
        }
        public void AddRepeatedContactThrowsException()
        {
            var original   = ContactEntityObjectMother.Random();
            var duplicated = ContactEntityObjectMother.Random();

            duplicated.Name = original.Name;

            repo.Setup(x => x.ExistsContactWithName(duplicated.Name, null)).Returns(true);

            var model = ContactsModelObjectMother.FromEntity(duplicated);

            var cmd     = new AddContactCommand(model);
            var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object);

            Assert.Throws <DomainException>(() => handler.Handle(cmd, new System.Threading.CancellationToken()));
        }
        public async void AddContactCommandTest()
        {
            //Arange
            var testHelper = new TestHelper();

            AddContactCommand        command = new AddContactCommand("someone", "*****@*****.**", "156464654", null);
            AddContactCommandHandler handler = new AddContactCommandHandler(testHelper.contactsContext, testHelper.GetInMemoryContactRepository(), testHelper.GetInMemoryUnitOfWork());

            //Act
            var insertedId = await handler.Handle(command, new System.Threading.CancellationToken());

            var contact = await testHelper.GetInMemoryContactRepository().GetByIdAsync(insertedId);

            //Asert
            insertedId.Should().BeGreaterThan(0);
            contact.Should().NotBeNull();
        }
Example #5
0
        public async void Adds_New_Contact_Successfully()
        {
            //Arrange
            var contact = new Contact
            {
                Id          = Guid.Empty,
                Name        = "Contact",
                Type        = "Client",
                Company     = "Contact co.",
                PhoneNumber = "000",
                Email       = "Contact@",
                Notes       = "ABC",
                Source      = "Social Media"
            };

            var user = new User
            {
                Id          = "1U",
                UserName    = "******",
                DisplayName = "TestUser ",
                Email       = "@test",
                Level       = "mid"
            };

            UserAccessor.Setup(x => x.GetLoggedUser()).ReturnsAsync(user).Verifiable();
            Mediator.Setup(x => x.Send(It.IsAny <AddContactCommand>(), new CancellationToken()))
            .ReturnsAsync(Unit.Value);

            //Act
            var addContactCommand = new AddContactCommand(contact.Name, contact.Type, contact.Company, contact.PhoneNumber,
                                                          contact.Email, contact.Notes);
            var handler = new AddContactCommandHandler(Context, UserAccessor.Object);
            var result  = await handler.Handle(addContactCommand, new CancellationToken());

            //Assert
            result.Should()
            .BeOfType <Unit>()
            .Equals(Unit.Value);
            UserAccessor.Verify();

            DbContextFactory.Destroy(Context);
        }
        public void AddContactCallsCollaborators()
        {
            var model = ContactsModelObjectMother.Random();

            repo.Setup(x => x.Add(It.IsAny <ContactEntity>()));

            uow.Setup(x => x.StartChanges());
            uow.Setup(x => x.CommitChanges());

            eventBus.Setup(x => x.Record(It.Is <ContactAddedDomainEvent>(p => p.FirstName == model.FirstName && p.LastName == model.LastName && p.AggregateRootId == model.Id)));
            eventBus.Setup(x => x.PublishAsync()).Returns(Task.Delay(500));

            var cmd     = new AddContactCommand(model);
            var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object);

            var x = handler.Handle(cmd, new System.Threading.CancellationToken()).Result;

            repo.VerifyAll();
            uow.VerifyAll();
            eventBus.VerifyAll();
        }