Skip to content

Commit 0398ee9

Browse files
authored
Merge pull request #28 from drewjcooper/more-iactionresult-assertions
Add assertions for Conflict IActionResults
2 parents 774648e + e28f85a commit 0398ee9

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

+42
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,48 @@ public BadRequestObjectResultAssertions BeBadRequestObjectResult(string reason =
431431
return new BadRequestObjectResultAssertions(Subject as BadRequestObjectResult);
432432
}
433433

434+
/// <summary>
435+
/// Asserts that the subject is an <see cref="ConflictResult"/>.
436+
/// </summary>
437+
/// <param name="reason">
438+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
439+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
440+
/// </param>
441+
/// <param name="reasonArgs">
442+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
443+
/// </param>
444+
[CustomAssertion]
445+
public ConflictResult BeConflictResult(string reason = "", params object[] reasonArgs)
446+
{
447+
Execute.Assertion
448+
.BecauseOf(reason, reasonArgs)
449+
.ForCondition(Subject is ConflictResult)
450+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(ConflictResult), Subject.GetType());
451+
452+
return Subject as ConflictResult;
453+
}
454+
455+
/// <summary>
456+
/// Asserts that the subject is a <see cref="ConflictObjectResult"/>.
457+
/// </summary>
458+
/// <param name="reason">
459+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
460+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
461+
/// </param>
462+
/// <param name="reasonArgs">
463+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
464+
/// </param>
465+
[CustomAssertion]
466+
public ConflictObjectResultAssertions BeConflictObjectResult(string reason = "", params object[] reasonArgs)
467+
{
468+
Execute.Assertion
469+
.BecauseOf(reason, reasonArgs)
470+
.ForCondition(Subject is ConflictObjectResult)
471+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(ConflictObjectResult), Subject.GetType());
472+
473+
return new ConflictObjectResultAssertions(Subject as ConflictObjectResult);
474+
}
475+
434476
/// <summary>
435477
/// Asserts that the subject is a <see cref="CreatedResult"/>.
436478
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Diagnostics;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc
7+
{
8+
/// <summary>
9+
/// Contains a number of methods to assert that a <see cref="ConflictObjectResult"/> is in the expected state.
10+
/// </summary>
11+
[DebuggerNonUserCode]
12+
public class ConflictObjectResultAssertions : ObjectAssertions
13+
{
14+
#region Public Constructors
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ConflictObjectResult" /> class.
17+
/// </summary>
18+
/// <param name="subject">The object to test assertion on</param>
19+
public ConflictObjectResultAssertions(ConflictObjectResult subject) : base(subject)
20+
{
21+
}
22+
23+
#endregion
24+
25+
#region Public Properties
26+
27+
/// <summary>
28+
/// The <see cref="ObjectResult.Value"/> property on the the tested <see cref="ConflictObjectResult"/>.
29+
/// </summary>
30+
public object Error => ConflictObjectResultSubject.Value;
31+
32+
/// <summary>
33+
/// The <see cref="ObjectResult.Value"/> property as <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/> on the the tested <see cref="ConflictObjectResult"/>.
34+
/// </summary>
35+
public SerializableError SerializableError => (SerializableError)ConflictObjectResultSubject.Value;
36+
#endregion
37+
38+
#region Private Properties
39+
private ConflictObjectResult ConflictObjectResultSubject => (ConflictObjectResult)Subject;
40+
41+
#endregion
42+
43+
#region Public Methods
44+
/// <summary>
45+
/// Asserts the error is of the expected type.
46+
/// </summary>
47+
/// <typeparam name="TError">The expected type.</typeparam>
48+
/// <returns>The typed error.</returns>
49+
public TError ErrorAs<TError>()
50+
{
51+
var error = Error;
52+
53+
if (error == null)
54+
Execute.Assertion
55+
.WithDefaultIdentifier("ConflictObjectResult.Error")
56+
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TError));
57+
58+
Execute.Assertion
59+
.ForCondition(error is TError)
60+
.WithDefaultIdentifier("ConflictObjectResult.Error")
61+
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TError), error.GetType());
62+
63+
return (TError)error;
64+
}
65+
66+
#endregion
67+
}
68+
}

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

+40
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,46 @@ public void BeBadRequestResult_GivenNotBadRequest_ShouldFail()
342342
.WithMessage(failureMessage);
343343
}
344344

345+
[Fact]
346+
public void BeConflictResult_GivenConflict_ShouldPass()
347+
{
348+
ActionResult result = new ConflictResult();
349+
350+
result.Should().BeConflictResult();
351+
}
352+
353+
[Fact]
354+
public void BeConflictResult_GivenNotConflict_ShouldFail()
355+
{
356+
ActionResult result = new ViewResult();
357+
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ConflictResult), typeof(ViewResult));
358+
359+
Action a = () => result.Should().BeConflictResult(Reason, ReasonArgs);
360+
361+
a.Should().Throw<Exception>()
362+
.WithMessage(failureMessage);
363+
}
364+
365+
[Fact]
366+
public void BeConflictObjectResult_GivenConflictObject_ShouldPass()
367+
{
368+
ActionResult result = new ConflictObjectResult("foo");
369+
370+
result.Should().BeConflictObjectResult();
371+
}
372+
373+
[Fact]
374+
public void BeConflictObjectResult_GivenNotConflictObject_ShouldFail()
375+
{
376+
ActionResult result = new ConflictResult();
377+
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ConflictObjectResult), typeof(ConflictResult));
378+
379+
Action a = () => result.Should().BeConflictObjectResult(Reason, ReasonArgs);
380+
381+
a.Should().Throw<Exception>()
382+
.WithMessage(failureMessage);
383+
}
384+
345385
[Fact]
346386
public void BeChallengeResult_GivenChallengeResult_ShouldPass()
347387
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using FluentAssertions.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.ModelBinding;
4+
using System;
5+
using Xunit;
6+
7+
namespace FluentAssertions.AspNetCore.Mvc.Tests
8+
{
9+
public class ConflictObjectResultAssertions_Tests
10+
{
11+
private const string TestError = "testError";
12+
13+
[Fact]
14+
public void Error_GivenConflictObjectResult_ShouldHaveTheSameError()
15+
{
16+
var result = new TestController().Conflict(TestError);
17+
result.Should().BeConflictObjectResult().Error.Should().BeSameAs(TestError);
18+
}
19+
20+
[Fact]
21+
public void SerializableError_GivenExpectedModelState_ShouldPass()
22+
{
23+
const string testErrorKey = "TestErrorKey";
24+
const string testErrorMessage = "TestErrorMessage";
25+
var testModelState = new ModelStateDictionary();
26+
testModelState.AddModelError(testErrorKey, testErrorMessage);
27+
var result = new TestController().Conflict(testModelState);
28+
29+
result.Should().BeConflictObjectResult().SerializableError.Should().ContainKey(testErrorKey);
30+
}
31+
32+
[Fact]
33+
public void ErrorAs_GivenExpectedError_ShouldPass()
34+
{
35+
var result = new TestController().Conflict(TestError);
36+
37+
result.Should().BeConflictObjectResult().ErrorAs<string>().Should().Be(TestError);
38+
}
39+
40+
[Fact]
41+
public void ErrorAs_GivenUnexpectedError_ShouldFail()
42+
{
43+
var result = new TestController().Conflict(TestError);
44+
45+
Action a = () => result.Should().BeConflictObjectResult().ErrorAs<string>().Should().Be("xyx");
46+
a.Should().Throw<Exception>();
47+
}
48+
49+
[Fact]
50+
public void ErrorAs_GivenWrongType_ShouldFail()
51+
{
52+
var result = new TestController().Conflict(TestError);
53+
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
54+
"ConflictObjectResult.Error", typeof(int), typeof(string));
55+
56+
Action a = () => result.Should().BeConflictObjectResult().ErrorAs<int>().Should().Be(2);
57+
58+
a.Should().Throw<Exception>().WithMessage(failureMessage);
59+
}
60+
61+
[Fact]
62+
public void ErrorAs_Null_ShouldFail()
63+
{
64+
ActionResult result = new ConflictObjectResult(null as object);
65+
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull(
66+
"ConflictObjectResult.Error", typeof(object));
67+
68+
Action a = () => result.Should().BeConflictObjectResult().ErrorAs<object>();
69+
70+
a.Should().Throw<Exception>().WithMessage(failureMessage);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)