Skip to content

Commit 6181927

Browse files
committed
Added expected exception messages for all unit tests with expected exceptions
1 parent afd1fce commit 6181927

File tree

13 files changed

+73
-36
lines changed

13 files changed

+73
-36
lines changed

MyWebApi.Tests/BuildersTests/ActionsTests/ShouldReturnOkResultTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ShouldReturnOkResultShouldNotThrowExceptionWithOkResult()
1919
[Test]
2020
[ExpectedException(
2121
typeof(HttpActionResultAssertionException),
22-
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be a OkNegotiatedContentResult<T>, but instead received a BadRequestResult.")]
22+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be OkNegotiatedContentResult<T>, but instead received BadRequestResult.")]
2323
public void ShouldReturnOkResultShouldThrowExceptionWithOtherThanOkResult()
2424
{
2525
MyWebApi

MyWebApi.Tests/BuildersTests/ActionsTests/ShouldReturnTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void ShouldReturnShouldNotThrowExceptionWithClassGenericDefinitionTypesAn
8787
[Test]
8888
[ExpectedException(
8989
typeof(HttpActionResultAssertionException),
90-
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be a ResponseModel, but instead received a List<ResponseModel>.")]
90+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ResponseModel, but instead received List<ResponseModel>.")]
9191
public void ShouldReturnShouldThrowExceptionWithDifferentResult()
9292
{
9393
MyWebApi
@@ -99,7 +99,7 @@ public void ShouldReturnShouldThrowExceptionWithDifferentResult()
9999
[Test]
100100
[ExpectedException(
101101
typeof(HttpActionResultAssertionException),
102-
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be a ResponseModel, but instead received a List<ResponseModel>.")]
102+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ResponseModel, but instead received List<ResponseModel>.")]
103103
public void ShouldReturnShouldThrowExceptionWithDifferentResultAndTypeOf()
104104
{
105105
MyWebApi
@@ -111,7 +111,7 @@ public void ShouldReturnShouldThrowExceptionWithDifferentResultAndTypeOf()
111111
[Test]
112112
[ExpectedException(
113113
typeof(HttpActionResultAssertionException),
114-
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be a ICollection<Int32>, but instead received a List<ResponseModel>.")]
114+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ICollection<Int32>, but instead received List<ResponseModel>.")]
115115
public void ShouldReturnShouldThrowExceptionWithDifferentGenericResult()
116116
{
117117
MyWebApi
@@ -123,7 +123,7 @@ public void ShouldReturnShouldThrowExceptionWithDifferentGenericResult()
123123
[Test]
124124
[ExpectedException(
125125
typeof(HttpActionResultAssertionException),
126-
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be a ICollection<Int32>, but instead received a List<ResponseModel>.")]
126+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ICollection<Int32>, but instead received List<ResponseModel>.")]
127127
public void ShouldReturnShouldThrowExceptionWithDifferentGenericResultAndTypeOf()
128128
{
129129
MyWebApi

MyWebApi.Tests/BuildersTests/ResponseModelsTests/ResponseModelErrorDetailsTestBuilderTests.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ public void ThatEqualsShouldNotThrowExceptionWhenProvidedMessageIsValid()
1919
.ShouldHaveModelStateFor<RequestModel>()
2020
.ContainingNoModelStateErrorFor(m => m.NonRequiredString)
2121
.ContainingModelStateErrorFor(m => m.RequiredString).ThatEquals("The RequiredString field is required.")
22-
.ContainingModelStateErrorFor(m => m.Integer).ThatEquals(string.Format("The field Integer must be between {0} and {1}.", 1, int.MaxValue));
22+
.ContainingModelStateErrorFor(m => m.RequiredString)
23+
.ContainingNoModelStateErrorFor(m => m.NotValidateInteger)
24+
.ContainingModelStateError("RequiredString")
25+
.ContainingModelStateErrorFor(m => m.Integer).ThatEquals(string.Format("The field Integer must be between {0} and {1}.", 1, int.MaxValue))
26+
.ContainingModelStateError("RequiredString")
27+
.ContainingModelStateError("Integer");
2328
}
2429

2530
[Test]
26-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
31+
[ExpectedException(
32+
typeof(ResponseModelErrorAssertionException),
33+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected error message for key RequiredString to be 'RequiredString field is required.', but instead found 'The RequiredString field is required.'.")]
2734
public void ThatEqualsShouldThrowExceptionWhenProvidedMessageIsValid()
2835
{
2936
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -52,7 +59,9 @@ public void BeginningWithShouldNotThrowExceptionWhenProvidedMessageIsValid()
5259
}
5360

5461
[Test]
55-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
62+
[ExpectedException(
63+
typeof(ResponseModelErrorAssertionException),
64+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected error message for key 'RequiredString' to start with 'RequiredString', but instead found 'The RequiredString field is required.'.")]
5665
public void BeginningWithShouldThrowExceptionWhenProvidedMessageIsValid()
5766
{
5867
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -81,7 +90,9 @@ public void EngingWithShouldNotThrowExceptionWhenProvidedMessageIsValid()
8190
}
8291

8392
[Test]
84-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
93+
[ExpectedException(
94+
typeof(ResponseModelErrorAssertionException),
95+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected error message for key 'RequiredString' to end with 'required!', but instead found 'The RequiredString field is required.'.")]
8596
public void EngingWithShouldThrowExceptionWhenProvidedMessageIsValid()
8697
{
8798
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -110,7 +121,9 @@ public void ContainingShouldNotThrowExceptionWhenProvidedMessageIsValid()
110121
}
111122

112123
[Test]
113-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
124+
[ExpectedException(
125+
typeof(ResponseModelErrorAssertionException),
126+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected error message for key 'RequiredString' to contain 'invalid', but instead found 'The RequiredString field is required.'.")]
114127
public void ContainingShouldThrowExceptionWhenProvidedMessageIsValid()
115128
{
116129
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

MyWebApi.Tests/BuildersTests/ResponseModelsTests/ResponseModelErrorTestBuilderTests.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Exceptions;
55

66
using NUnit.Framework;
7+
78
using Setups;
89
using Setups.Models;
910

@@ -24,7 +25,9 @@ public void ContainingNoErrorsShouldNotThrowExceptionWhenThereAreNoModelStateErr
2425
}
2526

2627
[Test]
27-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
28+
[ExpectedException(
29+
typeof(ResponseModelErrorAssertionException),
30+
ExpectedMessage = "When calling OkResultActionWithRequestBody action in WebApiController expected to have valid model state with no errors, but it had some.")]
2831
public void ContainingNoErrorsShouldThrowExceptionWhenThereAreModelStateErrors()
2932
{
3033
var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -51,7 +54,9 @@ public void AndModelStateErrorShouldNotThrowExceptionWhenTheProvidedModelStateEr
5154
}
5255

5356
[Test]
54-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
57+
[ExpectedException(
58+
typeof(ResponseModelErrorAssertionException),
59+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have a model error against key Name, but none found.")]
5560
public void AndModelStateErrorShouldThrowExceptionWhenTheProvidedModelStateErrorDoesNotExist()
5661
{
5762
var requestBody = TestObjectFactory.GetValidRequestModel();
@@ -78,7 +83,9 @@ public void AndModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyH
7883
}
7984

8085
[Test]
81-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
86+
[ExpectedException(
87+
typeof(ResponseModelErrorAssertionException),
88+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have a model error against key RequiredString, but none found.")]
8289
public void AndModelStateErrorForShouldThrowExceptionWhenTheProvidedPropertyDoesNotHaveErrors()
8390
{
8491
var requestBody = TestObjectFactory.GetValidRequestModel();
@@ -105,7 +112,9 @@ public void AndNoModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropert
105112
}
106113

107114
[Test]
108-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
115+
[ExpectedException(
116+
typeof(ResponseModelErrorAssertionException),
117+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have no model errors against key RequiredString, but found some.")]
109118
public void AndNoModelStateErrorForShouldThrowExceptionWhenTheProvidedPropertyHasErrors()
110119
{
111120
var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -133,7 +142,9 @@ public void AndNoModelStateErrorForShouldNotThrowExceptionWhenChainedWithValidMo
133142
}
134143

135144
[Test]
136-
[ExpectedException(typeof(ResponseModelErrorAssertionException))]
145+
[ExpectedException(
146+
typeof(ResponseModelErrorAssertionException),
147+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have no model errors against key Integer, but found some.")]
137148
public void AndNoModelStateErrorForShouldThrowExceptionWhenChainedWithInvalidModel()
138149
{
139150
var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

MyWebApi.Tests/BuildersTests/ResponseModelsTests/ResponseModelTestBuilderTests.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public void WithResponseModelShouldNotThrowExceptionWithIncorrectInheritedTypeAr
3232
}
3333

3434
[Test]
35-
[ExpectedException(typeof(ResponseModelAssertionException))]
35+
[ExpectedException(
36+
typeof(ResponseModelAssertionException),
37+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model to be a ResponseModel, but instead received a ICollection<ResponseModel>.")]
3638
public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
3739
{
3840
MyWebApi
@@ -43,7 +45,9 @@ public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
4345
}
4446

4547
[Test]
46-
[ExpectedException(typeof(ResponseModelAssertionException))]
48+
[ExpectedException(
49+
typeof(ResponseModelAssertionException),
50+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model to be a ICollection<Int32>, but instead received a ICollection<ResponseModel>.")]
4751
public void WithResponseModelShouldThrowExceptionWithIncorrectGenericTypeArgument()
4852
{
4953
MyWebApi
@@ -66,7 +70,9 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectPassedExpectedObj
6670
}
6771

6872
[Test]
69-
[ExpectedException(typeof(ResponseModelAssertionException))]
73+
[ExpectedException(
74+
typeof(ResponseModelAssertionException),
75+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model ICollection<ResponseModel> to be the given model, but in fact it was a different model.")]
7076
public void WithResponceModelShouldThrowExceptionWithDifferentPassedExpectedObject()
7177
{
7278
var controller = new WebApiController();
@@ -93,7 +99,8 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectAssertions()
9399
}
94100

95101
[Test]
96-
[ExpectedException(typeof(AssertionException))]
102+
[ExpectedException(
103+
typeof(AssertionException))]
97104
public void WithResponseModelShouldThrowExceptionWithIncorrectAssertions()
98105
{
99106
MyWebApi
@@ -118,7 +125,9 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectPredicate()
118125
}
119126

120127
[Test]
121-
[ExpectedException(typeof(ResponseModelAssertionException))]
128+
[ExpectedException(
129+
typeof(ResponseModelAssertionException),
130+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model IList<ResponseModel> to pass the given condition, but it failed.")]
122131
public void WithResponseModelShouldThrowExceptionWithWrongPredicate()
123132
{
124133
MyWebApi
@@ -139,7 +148,9 @@ public void WithNoResponseModelShouldNotThrowExceptionWhenNoResponseModel()
139148
}
140149

141150
[Test]
142-
[ExpectedException(typeof(ResponseModelAssertionException))]
151+
[ExpectedException(
152+
typeof(ResponseModelAssertionException),
153+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected to not have response model but in fact response model was found.")]
143154
public void WithNoResponseModelShouldThrowExceptionWhenResponseModelExists()
144155
{
145156
MyWebApi

MyWebApi.Tests/Setups/Models/RequestModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public class RequestModel
1111
public string RequiredString { get; set; }
1212

1313
public string NonRequiredString { get; set; }
14+
15+
public int NotValidateInteger { get; set; }
1416
}
1517
}

MyWebApi.Tests/UtilitiesTests/ExpressionParserTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void GetMethodNameShouldReturnCorrectMethodNameWithValidMethodCallExpress
2424
}
2525

2626
[Test]
27-
[ExpectedException(typeof(ArgumentException))]
27+
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Provided expression is not a valid method call.")]
2828
public void GetMethodNameShouldThrowArgumentExceptionWithInvalidMethodCallExpression()
2929
{
3030
Expression<Func<int>> expression = () => 0;
@@ -65,7 +65,7 @@ public void ResolveMethodArgumentsShouldReturnEmptyCollectionIfMethoDoesNotHaveA
6565
}
6666

6767
[Test]
68-
[ExpectedException(typeof(ArgumentException))]
68+
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Provided expression is not a valid method call.")]
6969
public void ResolveMethodArgumentsShouldThrowArgumentExceptionWithInvalidMethodCallExpression()
7070
{
7171
Expression<Func<int>> expression = () => 0;
@@ -82,7 +82,7 @@ public void GetPropertyNameShouldReturnProperMemberNameWithValidExpression()
8282
}
8383

8484
[Test]
85-
[ExpectedException(typeof(ArgumentException))]
85+
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Provided expression is not a valid member expression.")]
8686
public void GetPropertyNameShouldThrowExceptionWithInvalidMemberExpression()
8787
{
8888
Expression<Func<WebApiController, object>> expression = c => c.OkResultWithResponse();

MyWebApi.Tests/UtilitiesTests/ValidatorTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ public void CheckForNullReferenceShouldNotThrowExceptionWithNotNullObject()
2626
[ExpectedException(typeof(ArgumentNullException))]
2727
public void CheckForNotEmptyStringShouldThrowArgumentNullExceptionWithNullString()
2828
{
29-
Validator.CheckForNotEmptyString(null);
29+
Validator.CheckForNotWhiteSpaceString(null);
3030
}
3131

3232
[Test]
3333
[ExpectedException(typeof(ArgumentNullException))]
3434
public void CheckForNotEmptyStringShouldThrowArgumentNullExceptionWithEmptyString()
3535
{
36-
Validator.CheckForNotEmptyString(string.Empty);
36+
Validator.CheckForNotWhiteSpaceString(string.Empty);
3737
}
3838

3939
[Test]
4040
[ExpectedException(typeof(ArgumentNullException))]
4141
public void CheckForNotEmptyStringShouldThrowArgumentNullExceptionWithWhiteSpace()
4242
{
43-
Validator.CheckForNotEmptyString(" ");
43+
Validator.CheckForNotWhiteSpaceString(" ");
4444
}
4545

4646
[Test]
4747
public void CheckForNotEmptyStringShouldNotThrowExceptionWithNormalString()
4848
{
49-
Validator.CheckForNotEmptyString(new string('a', 10));
49+
Validator.CheckForNotWhiteSpaceString(new string('a', 10));
5050
}
5151
}
5252
}

MyWebApi/Builders/Actions/ActionResultTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void ValidateActionReturnType(Type typeOfExpectedReturnValue, bool canBe
5858
if (invalid)
5959
{
6060
throw new HttpActionResultAssertionException(string.Format(
61-
"When calling {0} action in {1} expected action result to be a {2}, but instead received a {3}.",
61+
"When calling {0} action in {1} expected action result to be {2}, but instead received {3}.",
6262
this.ActionName,
6363
this.Controller.GetType().ToFriendlyGenericTypeName(),
6464
typeOfExpectedReturnValue.ToFriendlyGenericTypeName(),

MyWebApi/Builders/Base/BaseTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public string ActionName
5656

5757
private set
5858
{
59-
Validator.CheckForNotEmptyString(value, errorMessageName: "ActionName");
59+
Validator.CheckForNotWhiteSpaceString(value, errorMessageName: "ActionName");
6060
this.actionName = value;
6161
}
6262
}

0 commit comments

Comments
 (0)