コード例 #1
0
        public async Task CreateSupplierValidation()
        {
            var validator = new CreateSupplierCommandValidator();

            var cmdValid = new CreateSupplierCommand
            {
                Name  = "Sali",
                Email = "*****@*****.**",
                Phone = "+41 79 456 45 45",
            };
            var resultValid = await validator.ValidateAsync(cmdValid);

            Assert.True(resultValid.IsValid);

            var cmdInvalidMail = new CreateSupplierCommand
            {
                Email = "not valid mail",
                Name  = "some name",
                Phone = "+41 79 456 45 45",
            };
            var resultInvalidMail = await validator.ValidateAsync(cmdInvalidMail);

            Assert.False(resultInvalidMail.IsValid);
            Assert.NotEmpty(resultInvalidMail.Errors);
            Assert.Contains(resultInvalidMail.Errors, e => e.PropertyName == nameof(cmdInvalidMail.Email));
            Assert.DoesNotContain(resultInvalidMail.Errors, e => e.PropertyName == nameof(cmdInvalidMail.Name));
            Assert.DoesNotContain(resultInvalidMail.Errors, e => e.PropertyName == nameof(cmdInvalidMail.Phone));

            var cmdMissingName = new CreateSupplierCommand
            {
                Email = "*****@*****.**",
                Phone = "+41 79 456 45 45",
            };
            var resultMissingName = await validator.ValidateAsync(cmdMissingName);

            Assert.False(resultMissingName.IsValid);
            Assert.Contains(resultMissingName.Errors, e => e.PropertyName == nameof(cmdMissingName.Name));
            Assert.DoesNotContain(resultMissingName.Errors, e => e.PropertyName == nameof(cmdMissingName.Email));
            Assert.DoesNotContain(resultMissingName.Errors, e => e.PropertyName == nameof(cmdMissingName.Phone));
        }
コード例 #2
0
        public async Task ValidateEmail(string email)
        {
            var createSupplierCommand = new CreateSupplierCommand
            {
                Name  = "Pascal",
                Phone = "+41 79 456 45 45",
                Email = email,
            };

            var createSupplierCommandValidator = new CreateSupplierCommandValidator();
            var result = await createSupplierCommandValidator.ValidateAsync(createSupplierCommand);

            Assert.False(result.IsValid);
            Assert.NotEmpty(result.Errors);
            Assert.Contains(result.Errors, e => e.PropertyName == nameof(createSupplierCommand.Email));
            Assert.DoesNotContain(result.Errors, e => e.PropertyName == nameof(createSupplierCommand.Name));
            Assert.DoesNotContain(result.Errors, e => e.PropertyName == nameof(createSupplierCommand.Phone));

            var updateSupplierCommand = new UpdateSupplierCommand
            {
                Id    = 1,
                Name  = "Pascal",
                Phone = "+41 79 456 45 45",
                Email = email,
            };
            var updateSupplierCommandValidator = new UpdateSupplierCommandValidator();

            result = await updateSupplierCommandValidator.ValidateAsync(updateSupplierCommand);

            Assert.False(result.IsValid);
            Assert.NotEmpty(result.Errors);
            Assert.Contains(result.Errors, e => e.PropertyName == nameof(updateSupplierCommand.Email));
            Assert.DoesNotContain(result.Errors, e => e.PropertyName == nameof(updateSupplierCommand.Id));
            Assert.DoesNotContain(result.Errors, e => e.PropertyName == nameof(updateSupplierCommand.Name));
            Assert.DoesNotContain(result.Errors, e => e.PropertyName == nameof(updateSupplierCommand.Phone));
        }