1: public class DataAnnotationsValidatorTestBase
2: {
3: protected static DtoTestClass Config = new DtoTestClass();
4:
5: public class DtoTestClass
6: {
7: [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Bitte den
Datenbanknamen angeben.")]
8: public string Database { get; set; }
9: public string Server { get; set; }
10: public string User { get; set; }
11: }
12: }
13:
14: public class When_an_invalid_object_is_passed : DataAnnotationsValidatorTestBase
15: {
16: Establish context = () =>
17: {
18: _validator = new DataAnnotationsValidator<DtoTestClass>();
19: Config.Database = string.Empty;
20:
21: };
22:
23: private Because of = () => _isValid = _validator.IsValid(Config);
24:
25: private It should_evaluate_it_as_invalid = () => _isValid.ShouldBeFalse();
26:
27: private static DataAnnotationsValidator<DtoTestClass> _validator;
28: private static bool _isValid = true;
29: }
30:
31: public class When_an_valid_object_is_passed : DataAnnotationsValidatorTestBase
32: {
33: Establish context = () =>
34: {
35: _validator = new DataAnnotationsValidator<DtoTestClass>();
36: Config.Database = "bac";
37:
38: };
39:
40: private Because of = () => _isValid = _validator.IsValid(Config);
41:
42: private It should_evaluate_it_as_valid = () => _isValid.ShouldBeTrue();
43:
44: private static DataAnnotationsValidator<DtoTestClass> _validator;
45: private static bool _isValid = false;
46: }
47:
48: public class When_an_object_has_no_annotations
49: {
50: internal class test
51: {
52:
53: }
54:
55: Establish context = () =>
56: {
57: _validator = new DataAnnotationsValidator<test>();
58:
59: };
60:
61: private Because of = () => _isValid = _validator.IsValid(new test());
62:
63: private It should_evaluate_it_as_valid = () => _isValid.ShouldBeTrue();
64:
65: private static DataAnnotationsValidator<test> _validator;
66: private static bool _isValid = false;
67: }
68:
69: public class When_an_object_with_one_invalid_property_is_passed :
DataAnnotationsValidatorTestBase
70: {
71: Establish context = () =>
72: {
73: _validator = new DataAnnotationsValidator<DtoTestClass>();
74: Config.Database = string.Empty;
75:
76: };
77:
78: private Because of = () => _messages = _validator.GetMessages(Config);
79:
80: private It should_evaluate_exactly_this_property_as_invalid = () =>
81: {
82: (_messages.Count == 1).ShouldBeTrue();
83: _messages.Aggregate(string.Empty, (current, message) => current + message)
84: .Contains("Datenbanknamen").ShouldBeTrue();
85: };
86:
87: private static DataAnnotationsValidator<DtoTestClass> _validator;
88: private static ICollection<string> _messages;
89: }