/
PersonTests.cs
153 lines (119 loc) · 4.14 KB
/
PersonTests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using BaseDataValidatorLibrary.Helpers;
using BaseModelsLibrary.Models;
using NetCoreUnitTestProject.Base;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFluent;
using static System.DateTime;
using BaseDataValidatorLibrary.LanguageExtensions;
namespace NetCoreUnitTestProject
{
/// <summary>
///
/// </summary>
[TestClass]
public partial class PersonTests : TestBase
{
[TestMethod]
[TestTraits(Trait.Annotations)]
public void ValidPerson()
{
// arrange
Person person = new ()
{
FirstName = "Mike",
LastName = "Flowers",
BirthDate = new DateTime(1932, Now.Month, Now.Day)
};
// act
EntityValidationResult result = Model.Validate(person);
// assert
Check.That(result.IsValid).IsTrue();
}
[TestMethod]
[TestTraits(Trait.Annotations)]
public void invalidFirstNamePerson()
{
// arrange
Person person = new()
{
FirstName = "Mi",
LastName = "Flowers",
BirthDate = new DateTime(1932, Now.Month, Now.Day)
};
// act
EntityValidationResult result = Model.Validate(person);
// assert
/*
* Here there are two possible assertions
*/
Check.That(result.IsValid).IsFalse();
Check.That(result.Errors.FirstOrDefault().ErrorMessage)
.ContainsExactly("Invalid FirstName");
}
[TestMethod]
[TestTraits(Trait.Annotations)]
public void InvalidDateValidPerson()
{
// arrange
DateTime date = new (2022, 4, 27);
Person person = new ()
{
FirstName = "Mike",
LastName = "Flowers",
BirthDate = new DateTime(1931, date.Month, date.Day)
};
// act
EntityValidationResult result = Model.Validate(person);
// assert
Check.That(result.IsValid).IsFalse();
}
/// <summary>
/// For <see cref="Model.GetValidateYearsErrorMessages(T)"/>
/// </summary>
[TestMethod]
[TestTraits(Trait.Annotations)]
public void ValidateYearsInformationInvalidDateValidPerson()
{
// arrange
DateTime date = new(2022, 4, 27);
Person person = new()
{
FirstName = "Mike",
LastName = "Flowers",
BirthDate = new DateTime(1931, date.Month, date.Day)
};
// act
var birthdayErrorInformation =
Model.GetValidateYearsErrorMessages(person)
.FirstOrDefault();
var errorMessage = person.GetAttribute<RequiredAttribute>("BirthDate").ErrorMessage;
// raw
Console.WriteLine(errorMessage);
// formatted
Console.WriteLine(errorMessage, "Birthdate");
// assert
Check.That(birthdayErrorInformation.ToString())
.Equals("Valid range for BirthDate is from 1932 to 2022");
}
/// <summary>
/// Different options for n-fluent asserts
/// </summary>
/// <remarks>
/// There are two flavors, first commented out which is brittle as the property name is
/// a string and thus if the property name changes the test will break while the
/// second un-commented method will not break when renaming in this case FirstName in
/// Visual Studio.
/// </remarks>
[TestMethod]
[TestTraits(Trait.Annotations)]
public void ListContainsExactlyTest()
{
object[] firstNames = { "Karen", "Mike", "Anne" };
Check.That(People.Extracting("FirstName")).ContainsExactly(firstNames);
Check.That(People.Extracting(nameof(Person.FirstName))).ContainsExactly(firstNames);
}
}
}