public void user_add_command_failed()
        {
            var fixture = new Fixture().Customize(new AutoNSubstituteCustomization {
                ConfigureMembers = true
            });

            var finalUser = fixture.Create <User>();
            var command   = fixture.Build <AddUserCommand>()
                            .With(user => user.CommercialEmail, finalUser.CommercialEmail.Value)
                            .With(user => user.Cnpj, finalUser.Cnpj.Value)
                            .With(user => user.Name, finalUser.Name.Value)
                            .Create();

            var mediator = fixture.Create <IMediator>();
            var logger   = fixture.Create <ILogger <AddUserCommandHandler> >();
            var db       = fixture.Create <IDbSession <IUserRepository> >();
            var handler  = new AddUserCommandHandler(logger, mediator, db);


            var result = handler.Execute(command);

            db.DidNotReceive().Repository.Add(finalUser);
            db.DidNotReceive().SaveChanges();

            Assert.True(!result.IsSucceed && result.Violations.Count == 3);
        }
            public void Execute_Should_Not_Add_Invalid_User()
            {
                AddUserCommandHandler sut = GetCommandHandler();

                AddUserCommand command = UserCommandHandlerTestHelper.GetAddCommand("a".PadLeft(101, 'a'));

                string expectedInvalid = string.Format(MessagesModel.MaxLength, "100");

                UserCommandResult result = sut.Execute(command);

                var calls = sut.WriteRepository.ReceivedCalls().Count();

                Assert.AreEqual(0, calls);
            }
        public void user_add_command_succed()
        {
            var fixture = new Fixture().Customize(new AutoNSubstituteCustomization {
                ConfigureMembers = true
            });

            fixture.Register <EntityId>(() => EntityId.From(fixture.Create <Guid>()));
            fixture.Register <Name>(() => Name.From(fixture.Create <string>()));
            fixture.Register <Version>(() => Version.From(fixture.Create <int>()));
            fixture.Register <SocialSecurityId>(() => SocialSecurityId.From(fixture.Create <string>()));
            fixture.Register <Email>(() => Email.From(string.Format($"{fixture.Create<string>()}@teste.com")));
            fixture.Register <User>(() => User.From(fixture.Create <EntityId>(),
                                                    fixture.Create <Name>(), fixture.Create <SocialSecurityId>(),
                                                    fixture.Create <Email>(), fixture.Create <Version>()));

            var finalUser = fixture.Create <User>();

            var command = fixture.Build <AddUserCommand>()
                          .With(user => user.CommercialEmail, finalUser.CommercialEmail.Value)
                          .With(user => user.Cnpj, finalUser.Cnpj.Value)
                          .With(user => user.Name, finalUser.Name.Value)
                          .Create();

            var mediator = fixture.Create <IMediator>();
            var logger   = fixture.Create <ILogger <AddUserCommandHandler> >();
            var db       = fixture.Create <IDbSession <IUserRepository> >();
            var handler  = new AddUserCommandHandler(logger, mediator, db);

            var result = handler.Execute(command);

            db.Received().Repository.Add(finalUser);
            db.Received().SaveChanges();
            mediator.Received(1).Publish(Arg.Any <UserAddedEvent>());

            Assert.True(result.IsSucceed);
        }