/
CustomMessageFormatTester.cs
89 lines (76 loc) · 3.07 KB
/
CustomMessageFormatTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#region License
// Copyright (c) .NET Foundation and contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
#endregion
namespace FluentValidation.Tests {
using System;
using System.Linq;
using Validators;
using Xunit;
public class CustomMessageFormatTester {
private TestValidator validator;
public CustomMessageFormatTester() {
validator = new TestValidator();
CultureScope.SetDefaultCulture();
}
[Fact]
public void Should_format_custom_message() {
const string expected = "Surname";
validator.RuleFor(x => x.Surname).NotNull().WithMessage("{PropertyName}");
string error = validator.Validate(new Person()).Errors.Single().ErrorMessage;
error.ShouldEqual(expected);
}
[Fact]
public void Uses_custom_delegate_for_building_message() {
validator.RuleFor(x => x.Surname).NotNull().Configure(cfg => {
cfg.MessageBuilder = context => "Test " + context.InstanceToValidate.Id;
});
var error = validator.Validate(new Person()).Errors.Single().ErrorMessage;
error.ShouldEqual("Test 0");
}
[Fact]
public void Uses_custom_delegate_for_building_message_only_for_specific_validator() {
validator.RuleFor(x => x.Surname).NotNull().NotEmpty().Configure(cfg => {
cfg.MessageBuilder = context => {
if (context.PropertyValidator is INotNullValidator)
return "Foo";
return context.GetDefaultMessage();
};
});
var result = validator.Validate(new Person());
result.Errors[0].ErrorMessage.ShouldEqual("Foo");
result.Errors[1].ErrorMessage.ShouldEqual("'Surname' must not be empty.");
}
[Fact]
public void Uses_property_value_in_message() {
validator.RuleFor(x => x.Surname).NotEqual("foo").WithMessage(person => $"was {person.Surname}");
var error = validator.Validate(new Person { Surname = "foo"}).Errors.Single().ErrorMessage;
error.ShouldEqual("was foo");
}
[Fact]
public void Replaces_propertyvalue_placeholder() {
validator.RuleFor(x => x.Email).EmailAddress().WithMessage("Was '{PropertyValue}'");
var result = validator.Validate(new Person() {Email = "foo"});
result.Errors.Single().ErrorMessage.ShouldEqual("Was 'foo'");
}
[Fact]
public void Replaces_propertyvalue_with_empty_string_when_null() {
validator.RuleFor(x => x.Surname).NotNull().WithMessage("Was '{PropertyValue}'");
var result = validator.Validate(new Person());
result.Errors.Single().ErrorMessage.ShouldEqual("Was ''");
}
}
}