Skip to content

Commit f818f7f

Browse files
authored
Merge pull request #14 from faddiv/ErrorMessagesFix
Error messages improvements
2 parents 4d49723 + 2ba35e7 commit f818f7f

File tree

66 files changed

+2221
-2335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2221
-2335
lines changed

samples/FluentAssertions.AspNetCore.Mvc.Sample/Controllers/ApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public IActionResult GetPhysicalFile(string physicalPath, string contentType, st
378378
[HttpGet]
379379
public IActionResult GetVirtualFileResult(string fileName, string contentType)
380380
{
381-
return new VirtualFileResult(fileName, contentType);
381+
return File(fileName, contentType);
382382
}
383383

384384
[HttpGet]

src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AcceptedAtActionResultAssertions(AcceptedAtActionResult subject) : base(s
2626
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
2727
/// </param>
2828
/// <param name="reasonArgs">
29-
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
29+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
3030
/// </param>
3131
public AcceptedAtActionResultAssertions WithActionName(string expectedActionName, string reason = "", params object[] reasonArgs)
3232
{
@@ -35,7 +35,8 @@ public AcceptedAtActionResultAssertions WithActionName(string expectedActionName
3535
Execute.Assertion
3636
.ForCondition(string.Equals(actualActionName, expectedActionName, StringComparison.OrdinalIgnoreCase))
3737
.BecauseOf(reason, reasonArgs)
38-
.FailWith("Expected AcceptedAtActionResult.ActionName to be {0}{reason} but was {1}", expectedActionName, actualActionName);
38+
.WithDefaultIdentifier("AcceptedAtActionResult.ActionName")
39+
.FailWith(FailureMessages.CommonFailMessage, expectedActionName, actualActionName);
3940

4041
return this;
4142
}
@@ -45,11 +46,11 @@ public AcceptedAtActionResultAssertions WithActionName(string expectedActionName
4546
/// </summary>
4647
/// <param name="expectedControllerName">The expected controller.</param>
4748
/// <param name="reason">
48-
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
49+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
4950
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
5051
/// </param>
5152
/// <param name="reasonArgs">
52-
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
53+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
5354
/// </param>
5455
public AcceptedAtActionResultAssertions WithControllerName(string expectedControllerName, string reason = "", params object[] reasonArgs)
5556
{
@@ -58,7 +59,8 @@ public AcceptedAtActionResultAssertions WithControllerName(string expectedContro
5859
Execute.Assertion
5960
.ForCondition(string.Equals(actualControllerName, expectedControllerName, StringComparison.OrdinalIgnoreCase))
6061
.BecauseOf(reason, reasonArgs)
61-
.FailWith("Expected AcceptedAtActionResult.ControllerName to be {0}{reason} but was {1}", expectedControllerName, actualControllerName);
62+
.WithDefaultIdentifier("AcceptedAtActionResult.ControllerName")
63+
.FailWith(FailureMessages.CommonFailMessage, expectedControllerName, actualControllerName);
6264

6365
return this;
6466
}
@@ -69,27 +71,18 @@ public AcceptedAtActionResultAssertions WithControllerName(string expectedContro
6971
/// <param name="key">The expected key.</param>
7072
/// <param name="expectedValue">The expected value.</param>
7173
/// <param name="reason">
72-
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
74+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
7375
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
7476
/// </param>
7577
/// <param name="reasonArgs">
76-
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
78+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
7779
/// </param>
7880
public AcceptedAtActionResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs)
7981
{
8082
var subjectTyped = Subject as AcceptedAtActionResult;
8183

82-
Execute.Assertion
83-
.ForCondition(subjectTyped != null && subjectTyped.RouteValues.ContainsKey(key))
84-
.BecauseOf(reason, reasonArgs)
85-
.FailWith(FailureMessages.AcceptedAtActionResult_RouteValues_ContainsKey, key);
86-
87-
var actualValue = subjectTyped.RouteValues[key];
88-
89-
Execute.Assertion
90-
.ForCondition(expectedValue.Equals(actualValue))
91-
.BecauseOf(reason, reasonArgs)
92-
.FailWith(FailureMessages.AcceptedAtActionResult_RouteValues_HaveValue, key, expectedValue, actualValue);
84+
AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues,
85+
"AcceptedAtActionResult.RouteValues", key, expectedValue, reason, reasonArgs);
9386

9487
return this;
9588
}
@@ -105,11 +98,14 @@ public TValue ValueAs<TValue>()
10598
var value = subjectTyped.Value;
10699

107100
if (value == null)
108-
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "AcceptedAtActionResult.Value", typeof(TValue).Name);
101+
Execute.Assertion
102+
.WithDefaultIdentifier("AcceptedAtActionResult.Value")
103+
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue));
109104

110105
Execute.Assertion
111106
.ForCondition(value is TValue)
112-
.FailWith(FailureMessages.CommonTypeFailMessage, "AcceptedAtActionResult.Value", typeof(TValue).Name, value.GetType().Name);
107+
.WithDefaultIdentifier("AcceptedAtActionResult.Value")
108+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType());
113109

114110
return (TValue)value;
115111
}

src/FluentAssertions.AspNetCore.Mvc/AcceptedAtRouteResultAssertions.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AcceptedAtRouteResultAssertions(AcceptedAtRouteResult subject) : base(sub
2626
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
2727
/// </param>
2828
/// <param name="reasonArgs">
29-
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
29+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
3030
/// </param>
3131
public AcceptedAtRouteResultAssertions WithRouteName(string expectedRouteName, string reason = "", params object[] reasonArgs)
3232
{
@@ -35,7 +35,8 @@ public AcceptedAtRouteResultAssertions WithRouteName(string expectedRouteName, s
3535
Execute.Assertion
3636
.BecauseOf(reason, reasonArgs)
3737
.ForCondition(string.Equals(expectedRouteName, subjectTyped.RouteName, StringComparison.OrdinalIgnoreCase))
38-
.FailWith(FailureMessages.CommonFailMessage, "AcceptedAtRouteResult.RouteName", expectedRouteName, subjectTyped.RouteName);
38+
.WithDefaultIdentifier("AcceptedAtRouteResult.RouteName")
39+
.FailWith(FailureMessages.CommonFailMessage, expectedRouteName, subjectTyped.RouteName);
3940

4041
return this;
4142
}
@@ -50,23 +51,14 @@ public AcceptedAtRouteResultAssertions WithRouteName(string expectedRouteName, s
5051
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
5152
/// </param>
5253
/// <param name="reasonArgs">
53-
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
54+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
5455
/// </param>
5556
public AcceptedAtRouteResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs)
5657
{
5758
var subjectTyped = Subject as AcceptedAtRouteResult;
5859

59-
Execute.Assertion
60-
.ForCondition(subjectTyped != null && subjectTyped.RouteValues.ContainsKey(key))
61-
.BecauseOf(reason, reasonArgs)
62-
.FailWith(FailureMessages.AcceptedAtRouteResult_RouteValues_ContainsKey, key);
63-
64-
var actualValue = subjectTyped.RouteValues[key];
65-
66-
Execute.Assertion
67-
.ForCondition(expectedValue.Equals(actualValue))
68-
.BecauseOf(reason, reasonArgs)
69-
.FailWith(FailureMessages.AcceptedAtRouteResult_RouteValues_HaveValue, key, expectedValue, actualValue);
60+
AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues,
61+
"AcceptedAtRouteResult.RouteValues", key, expectedValue, reason, reasonArgs);
7062

7163
return this;
7264
}
@@ -82,11 +74,14 @@ public TValue ValueAs<TValue>()
8274
var value = subjectTyped.Value;
8375

8476
if (value == null)
85-
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "AcceptedAtRouteResult.Value", typeof(TValue).Name);
77+
Execute.Assertion
78+
.WithDefaultIdentifier("AcceptedAtRouteResult.Value")
79+
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue));
8680

8781
Execute.Assertion
8882
.ForCondition(value is TValue)
89-
.FailWith(FailureMessages.CommonTypeFailMessage, "AcceptedAtRouteResult.Value", typeof(TValue).Name, value.GetType().Name);
83+
.WithDefaultIdentifier("AcceptedAtRouteResult.Value")
84+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType());
9085

9186
return (TValue)value;
9287
}

src/FluentAssertions.AspNetCore.Mvc/AcceptedResultAssertions.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ public TValue ValueAs<TValue>()
5050
var value = AcceptedResultSubject.Value;
5151

5252
if (value == null)
53-
Execute.Assertion.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, "AcceptedResultAssertions.Value", typeof(TValue).Name);
53+
Execute.Assertion
54+
.WithDefaultIdentifier("AcceptedResultAssertions.Value")
55+
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue));
5456

5557
Execute.Assertion
5658
.ForCondition(value is TValue)
57-
.FailWith(FailureMessages.CommonTypeFailMessage, "AcceptedResultAssertions.Value", typeof(TValue).Name, value.GetType().Name);
59+
.WithDefaultIdentifier("AcceptedResultAssertions.Value")
60+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType());
5861

5962
return (TValue)value;
6063
}
@@ -65,16 +68,23 @@ public TValue ValueAs<TValue>()
6568
/// <param name="uri">
6669
/// The Uri.
6770
/// </param>
71+
/// <param name="reason">
72+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
73+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
74+
/// </param>
75+
/// <param name="reasonArgs">
76+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
77+
/// </param>
6878
/// <returns>The typed value.</returns>
69-
public AcceptedResultAssertions WithUri(Uri uri)
79+
public AcceptedResultAssertions WithUri(Uri uri, string reason = "", params object[] reasonArgs)
7080
{
71-
var expectedUri = !uri.IsAbsoluteUri
72-
? uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped)
73-
: uri.AbsoluteUri;
81+
var expectedUri = AssertionHelpers.GetAbsoluteUri(uri);
7482

7583
Execute.Assertion
84+
.BecauseOf(reason, reasonArgs)
7685
.ForCondition(expectedUri == Location)
77-
.FailWith(FailureMessages.CommonFailMessage, "AcceptedResultAssertions.Uri", expectedUri, Location);
86+
.WithDefaultIdentifier("AcceptedResultAssertions.Uri")
87+
.FailWith(FailureMessages.CommonFailMessage, expectedUri, Location);
7888

7989
return this;
8090
}
@@ -85,12 +95,21 @@ public AcceptedResultAssertions WithUri(Uri uri)
8595
/// <param name="uri">
8696
/// The Uri as string.
8797
/// </param>
98+
/// <param name="reason">
99+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
100+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
101+
/// </param>
102+
/// <param name="reasonArgs">
103+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
104+
/// </param>
88105
/// <returns>The typed value.</returns>
89-
public AcceptedResultAssertions WithUri(string uri)
106+
public AcceptedResultAssertions WithUri(string uri, string reason = "", params object[] reasonArgs)
90107
{
91108
Execute.Assertion
109+
.BecauseOf(reason, reasonArgs)
92110
.ForCondition(uri == Location)
93-
.FailWith(FailureMessages.CommonFailMessage, "AcceptedResultAssertions.Uri", uri, Location);
111+
.WithDefaultIdentifier("AcceptedResultAssertions.Uri")
112+
.FailWith(FailureMessages.CommonFailMessage, uri, Location);
94113

95114
return this;
96115
}

0 commit comments

Comments
 (0)