|
| 1 | +using System; |
| 2 | +using FluentAssertions.Mvc.Tests.Helpers; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.AspNetCore.Mvc.Formatters; |
| 5 | +using Moq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace FluentAssertions.AspNetCore.Mvc.Tests |
| 9 | +{ |
| 10 | + public class ObjectResultAssertions_Tests |
| 11 | + { |
| 12 | + private const string TestValue = "testValue"; |
| 13 | + public const string Reason = FailureMessageHelper.Reason; |
| 14 | + public readonly static object[] ReasonArgs = FailureMessageHelper.ReasonArgs; |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Value_GivenObjectResult_ShouldHaveTheSameValue() |
| 18 | + { |
| 19 | + var result = new ObjectResult(TestValue); |
| 20 | + result.Should().BeObjectResult().Value.Should().BeSameAs(TestValue); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void ValueAs_GivenObjectResult_ShouldHaveTheSameValue() |
| 25 | + { |
| 26 | + var result = new ObjectResult(TestValue); |
| 27 | + |
| 28 | + result.Should().BeObjectResult().ValueAs<string>().Should().BeSameAs(TestValue); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void ValueAs_GivenWrongType_ShouldFail() |
| 33 | + { |
| 34 | + var result = new ObjectResult(TestValue); |
| 35 | + string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY( |
| 36 | + "ObjectResult.Value", typeof(int), typeof(string)); |
| 37 | + |
| 38 | + Action a = () => result.Should().BeObjectResult().ValueAs<int>().Should().Be(2); |
| 39 | + |
| 40 | + a.Should().Throw<Exception>() |
| 41 | + .WithMessage(failureMessage); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void ValueAs_Null_ShouldFail() |
| 46 | + { |
| 47 | + ActionResult result = new ObjectResult(null); |
| 48 | + string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull( |
| 49 | + "ObjectResult.Value", typeof(object)); |
| 50 | + |
| 51 | + Action a = () => result.Should().BeObjectResult().ValueAs<object>(); |
| 52 | + |
| 53 | + a.Should().Throw<Exception>() |
| 54 | + .WithMessage(failureMessage); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void ContainsFormatter_GivenExpected_ShouldPass() |
| 59 | + { |
| 60 | + var formatterMock = new Mock<IOutputFormatter>(); |
| 61 | + var result = new ObjectResult(TestValue) |
| 62 | + { |
| 63 | + Formatters = { formatterMock.Object } |
| 64 | + }; |
| 65 | + |
| 66 | + result.Should().BeObjectResult().ContainsFormatter(formatter => ReferenceEquals(formatter, formatterMock.Object)); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void ContainsFormatter_OnNullFormatters_ShouldFail() |
| 71 | + { |
| 72 | + var result = new ObjectResult(TestValue) |
| 73 | + { |
| 74 | + Formatters = null |
| 75 | + }; |
| 76 | + string failureMessage = FailureMessageHelper.ExpectedToContainItemButFoundNull( |
| 77 | + "ObjectResult.Formatters", |
| 78 | + "(formatter == null)"); |
| 79 | + |
| 80 | + Action a = () => result.Should().BeObjectResult().ContainsFormatter(formatter => formatter == null, Reason, ReasonArgs); |
| 81 | + |
| 82 | + a.Should().Throw<Exception>() |
| 83 | + .WithMessage(failureMessage); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void ContainsFormatter_GivenUnexpected_ShouldFail() |
| 88 | + { |
| 89 | + var formatterMock = new Mock<IOutputFormatter>(); |
| 90 | + var result = new ObjectResult(TestValue) |
| 91 | + { |
| 92 | + Formatters = { formatterMock.Object } |
| 93 | + }; |
| 94 | + string failureMessage = FailureMessageHelper.ExpectedToHaveItemMatching( |
| 95 | + "ObjectResult.Formatters", |
| 96 | + result.Formatters, |
| 97 | + "False"); |
| 98 | + |
| 99 | + Action a = () => result.Should().BeObjectResult().ContainsFormatter(formatter => false, Reason, ReasonArgs); |
| 100 | + |
| 101 | + a.Should().Throw<Exception>() |
| 102 | + .WithMessage(failureMessage); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments