Skip to content

Add assertions for Conflict IActionResults #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,48 @@ public BadRequestObjectResultAssertions BeBadRequestObjectResult(string reason =
return new BadRequestObjectResultAssertions(Subject as BadRequestObjectResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="ConflictResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
[CustomAssertion]
public ConflictResult BeConflictResult(string reason = "", params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is ConflictResult)
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(ConflictResult), Subject.GetType());

return Subject as ConflictResult;
}

/// <summary>
/// Asserts that the subject is a <see cref="ConflictObjectResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
[CustomAssertion]
public ConflictObjectResultAssertions BeConflictObjectResult(string reason = "", params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is ConflictObjectResult)
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(ConflictObjectResult), Subject.GetType());

return new ConflictObjectResultAssertions(Subject as ConflictObjectResult);
}

/// <summary>
/// Asserts that the subject is a <see cref="CreatedResult"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace FluentAssertions.AspNetCore.Mvc
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="ConflictObjectResult"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class ConflictObjectResultAssertions : ObjectAssertions
{
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ConflictObjectResult" /> class.
/// </summary>
/// <param name="subject">The object to test assertion on</param>
public ConflictObjectResultAssertions(ConflictObjectResult subject) : base(subject)
{
}

#endregion

#region Public Properties

/// <summary>
/// The <see cref="ObjectResult.Value"/> property on the the tested <see cref="ConflictObjectResult"/>.
/// </summary>
public object Error => ConflictObjectResultSubject.Value;

/// <summary>
/// The <see cref="ObjectResult.Value"/> property as <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/> on the the tested <see cref="ConflictObjectResult"/>.
/// </summary>
public SerializableError SerializableError => (SerializableError)ConflictObjectResultSubject.Value;
#endregion

#region Private Properties
private ConflictObjectResult ConflictObjectResultSubject => (ConflictObjectResult)Subject;

#endregion

#region Public Methods
/// <summary>
/// Asserts the error is of the expected type.
/// </summary>
/// <typeparam name="TError">The expected type.</typeparam>
/// <returns>The typed error.</returns>
public TError ErrorAs<TError>()
{
var error = Error;

if (error == null)
Execute.Assertion
.WithDefaultIdentifier("ConflictObjectResult.Error")
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TError));

Execute.Assertion
.ForCondition(error is TError)
.WithDefaultIdentifier("ConflictObjectResult.Error")
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TError), error.GetType());

return (TError)error;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,46 @@ public void BeBadRequestResult_GivenNotBadRequest_ShouldFail()
.WithMessage(failureMessage);
}

[Fact]
public void BeConflictResult_GivenConflict_ShouldPass()
{
ActionResult result = new ConflictResult();

result.Should().BeConflictResult();
}

[Fact]
public void BeConflictResult_GivenNotConflict_ShouldFail()
{
ActionResult result = new ViewResult();
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ConflictResult), typeof(ViewResult));

Action a = () => result.Should().BeConflictResult(Reason, ReasonArgs);

a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}

[Fact]
public void BeConflictObjectResult_GivenConflictObject_ShouldPass()
{
ActionResult result = new ConflictObjectResult("foo");

result.Should().BeConflictObjectResult();
}

[Fact]
public void BeConflictObjectResult_GivenNotConflictObject_ShouldFail()
{
ActionResult result = new ConflictResult();
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason("result", typeof(ConflictObjectResult), typeof(ConflictResult));

Action a = () => result.Should().BeConflictObjectResult(Reason, ReasonArgs);

a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}

[Fact]
public void BeChallengeResult_GivenChallengeResult_ShouldPass()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using FluentAssertions.Mvc.Tests.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using Xunit;

namespace FluentAssertions.AspNetCore.Mvc.Tests
{
public class ConflictObjectResultAssertions_Tests
{
private const string TestError = "testError";

[Fact]
public void Error_GivenConflictObjectResult_ShouldHaveTheSameError()
{
var result = new TestController().Conflict(TestError);
result.Should().BeConflictObjectResult().Error.Should().BeSameAs(TestError);
}

[Fact]
public void SerializableError_GivenExpectedModelState_ShouldPass()
{
const string testErrorKey = "TestErrorKey";
const string testErrorMessage = "TestErrorMessage";
var testModelState = new ModelStateDictionary();
testModelState.AddModelError(testErrorKey, testErrorMessage);
var result = new TestController().Conflict(testModelState);

result.Should().BeConflictObjectResult().SerializableError.Should().ContainKey(testErrorKey);
}

[Fact]
public void ErrorAs_GivenExpectedError_ShouldPass()
{
var result = new TestController().Conflict(TestError);

result.Should().BeConflictObjectResult().ErrorAs<string>().Should().Be(TestError);
}

[Fact]
public void ErrorAs_GivenUnexpectedError_ShouldFail()
{
var result = new TestController().Conflict(TestError);

Action a = () => result.Should().BeConflictObjectResult().ErrorAs<string>().Should().Be("xyx");
a.Should().Throw<Exception>();
}

[Fact]
public void ErrorAs_GivenWrongType_ShouldFail()
{
var result = new TestController().Conflict(TestError);
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
"ConflictObjectResult.Error", typeof(int), typeof(string));

Action a = () => result.Should().BeConflictObjectResult().ErrorAs<int>().Should().Be(2);

a.Should().Throw<Exception>().WithMessage(failureMessage);
}

[Fact]
public void ErrorAs_Null_ShouldFail()
{
ActionResult result = new ConflictObjectResult(null as object);
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull(
"ConflictObjectResult.Error", typeof(object));

Action a = () => result.Should().BeConflictObjectResult().ErrorAs<object>();

a.Should().Throw<Exception>().WithMessage(failureMessage);
}
}
}