Skip to content

Commit c91edc5

Browse files
committed
Refactor CreatedAtActionResultAssertions to use ObjectResultAssertionsBase.
1 parent df3b2b2 commit c91edc5

File tree

3 files changed

+13
-37
lines changed

3 files changed

+13
-37
lines changed

src/FluentAssertions.AspNetCore.Mvc/AcceptedAtActionResultAssertions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public AcceptedAtActionResultAssertions(AcceptedAtActionResult subject) : base(s
2929
/// </param>
3030
public AcceptedAtActionResultAssertions WithActionName(string expectedActionName, string reason = "", params object[] reasonArgs)
3131
{
32-
string actualActionName = ObjectResultSubject?.ActionName;
32+
string actualActionName = ObjectResultSubject.ActionName;
3333

3434
Execute.Assertion
3535
.ForCondition(string.Equals(actualActionName, expectedActionName, StringComparison.OrdinalIgnoreCase))
@@ -53,7 +53,7 @@ public AcceptedAtActionResultAssertions WithActionName(string expectedActionName
5353
/// </param>
5454
public AcceptedAtActionResultAssertions WithControllerName(string expectedControllerName, string reason = "", params object[] reasonArgs)
5555
{
56-
string actualControllerName = ObjectResultSubject?.ControllerName;
56+
string actualControllerName = ObjectResultSubject.ControllerName;
5757

5858
Execute.Assertion
5959
.ForCondition(string.Equals(actualControllerName, expectedControllerName, StringComparison.OrdinalIgnoreCase))

src/FluentAssertions.AspNetCore.Mvc/CreatedAtActionResultAssertions.cs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
using System;
2-
using System.Diagnostics;
3-
using FluentAssertions.Execution;
4-
using FluentAssertions.Primitives;
1+
using FluentAssertions.Execution;
52
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using System.Diagnostics;
65

76
namespace FluentAssertions.AspNetCore.Mvc
87
{
98
/// <summary>
109
/// Contains a number of methods to assert that a <see cref="CreatedAtActionResult"/> is in the expected state.
1110
/// </summary>
1211
[DebuggerNonUserCode]
13-
public class CreatedAtActionResultAssertions : ObjectAssertions
12+
public class CreatedAtActionResultAssertions : ObjectResultAssertionsBase<CreatedAtActionResult, CreatedAtActionResultAssertions>
1413
{
1514
/// <summary>
1615
/// Initializes a new instance of the <see cref="CreatedAtActionResultAssertions" /> class.
@@ -30,7 +29,7 @@ public CreatedAtActionResultAssertions(CreatedAtActionResult subject) : base(sub
3029
/// </param>
3130
public CreatedAtActionResultAssertions WithActionName(string expectedActionName, string reason = "", params object[] reasonArgs)
3231
{
33-
string actualActionName = (Subject as CreatedAtActionResult)?.ActionName;
32+
string actualActionName = ObjectResultSubject.ActionName;
3433

3534
Execute.Assertion
3635
.ForCondition(string.Equals(actualActionName, expectedActionName, StringComparison.OrdinalIgnoreCase))
@@ -54,7 +53,7 @@ public CreatedAtActionResultAssertions WithActionName(string expectedActionName,
5453
/// </param>
5554
public CreatedAtActionResultAssertions WithControllerName(string expectedControllerName, string reason = "", params object[] reasonArgs)
5655
{
57-
string actualControllerName = (Subject as CreatedAtActionResult)?.ControllerName;
56+
string actualControllerName = ObjectResultSubject.ControllerName;
5857

5958
Execute.Assertion
6059
.ForCondition(string.Equals(actualControllerName, expectedControllerName, StringComparison.OrdinalIgnoreCase))
@@ -79,35 +78,12 @@ public CreatedAtActionResultAssertions WithControllerName(string expectedControl
7978
/// </param>
8079
public CreatedAtActionResultAssertions WithRouteValue(string key, object expectedValue, string reason = "", params object[] reasonArgs)
8180
{
82-
var subjectTyped = Subject as CreatedAtActionResult;
81+
var actualRouteValues = ObjectResultSubject.RouteValues;
8382

84-
AssertionHelpers.AssertStringObjectDictionary(subjectTyped.RouteValues, "CreatedAtActionResult.RouteValues",
83+
AssertionHelpers.AssertStringObjectDictionary(actualRouteValues, "CreatedAtActionResult.RouteValues",
8584
key, expectedValue, reason, reasonArgs);
8685

8786
return this;
8887
}
89-
90-
/// <summary>
91-
/// Asserts the value is of the expected type.
92-
/// </summary>
93-
/// <typeparam name="TValue">The expected type.</typeparam>
94-
/// <returns>The typed value.</returns>
95-
public TValue ValueAs<TValue>()
96-
{
97-
var subjectTyped = Subject as CreatedAtActionResult;
98-
var value = subjectTyped.Value;
99-
100-
if (value == null)
101-
Execute.Assertion
102-
.WithDefaultIdentifier("CreatedAtActionResult.Value")
103-
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue));
104-
105-
Execute.Assertion
106-
.ForCondition(value is TValue)
107-
.WithDefaultIdentifier("CreatedAtActionResult.Value")
108-
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType());
109-
110-
return (TValue)value;
111-
}
11288
}
11389
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/CreatedAtActionResultAssertions_Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using FluentAssertions.Mvc.Tests.Helpers;
1+
using FluentAssertions.Mvc.Tests.Helpers;
32
using Microsoft.AspNetCore.Mvc;
3+
using System;
44
using Xunit;
55

66
namespace FluentAssertions.AspNetCore.Mvc.Tests
77
{
8-
8+
99
public class CreatedAtActionResultAssertions_Tests
1010
{
1111
public const string Reason = FailureMessageHelper.Reason;

0 commit comments

Comments
 (0)