diff --git a/.gitignore b/.gitignore index f846a11..a8a1e75 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ TestResult.xml [Pp]ackages/ *.nupkg /.vs +_ReSharper.Caches \ No newline at end of file diff --git a/src/FluentAssertions.Mvc.Shared/ViewResultBaseAssertions.cs b/src/FluentAssertions.Mvc.Shared/ViewResultBaseAssertions.cs index bcb125a..d890f81 100644 --- a/src/FluentAssertions.Mvc.Shared/ViewResultBaseAssertions.cs +++ b/src/FluentAssertions.Mvc.Shared/ViewResultBaseAssertions.cs @@ -71,7 +71,7 @@ public ViewResultBaseAssertions WithViewData(string key, object expectedValue var actualValue = actualViewData[key]; Execute.Assertion - .ForCondition(actualValue.Equals(expectedValue)) + .ForCondition(object.Equals(actualValue, expectedValue)) .BecauseOf(reason, reasonArgs) .FailWith(FailureMessages.ViewResultBase_ViewData_HaveValue, key, expectedValue, actualValue); diff --git a/tests/FluentAssertions.Mvc.Tests.Shared/Helpers/FailureMessageHelper.cs b/tests/FluentAssertions.Mvc.Tests.Shared/Helpers/FailureMessageHelper.cs index 0f88101..8bc5c94 100644 --- a/tests/FluentAssertions.Mvc.Tests.Shared/Helpers/FailureMessageHelper.cs +++ b/tests/FluentAssertions.Mvc.Tests.Shared/Helpers/FailureMessageHelper.cs @@ -8,8 +8,8 @@ namespace FluentAssertions.Mvc.Tests.Helpers static class FailureMessageHelper { public static string Format(string message, params string[] args) - { - var formattedArg = args.Select(x => String.Format("\"{0}\"", x)).ToArray(); + { + var formattedArg = args.Select(x => (object)(x == null ? "" : string.Format("\"{0}\"", x))).ToArray(); return String.Format(message, formattedArg); } diff --git a/tests/FluentAssertions.Mvc.Tests.Shared/ViewResultAssertions_Tests.cs b/tests/FluentAssertions.Mvc.Tests.Shared/ViewResultAssertions_Tests.cs index 9e06cc4..6b361f0 100644 --- a/tests/FluentAssertions.Mvc.Tests.Shared/ViewResultAssertions_Tests.cs +++ b/tests/FluentAssertions.Mvc.Tests.Shared/ViewResultAssertions_Tests.cs @@ -206,6 +206,31 @@ public void WithViewData_GivenUnexpectedValue_ShouldFail() .WithMessage(failureMessage); } + [Test] + public void WithViewData_GivenUnexpectedValue_ActualValueIsNull_ShouldFail() + { + var key = "key1"; + const object actualValue = null; + var expectedValue = "abc"; + var failureMessage = FailureMessageHelper.Format(FailureMessages.ViewResultBase_ViewData_HaveValue, key, expectedValue, null); + +#if NETCOREAPP1_0 + var result = new TestController().ViewWithOneViewDataWhereValueIsNull(); // Where is TestController defined? +#else + ActionResult result = new ViewResult + { + ViewData = new ViewDataDictionary { { key, actualValue } } + }; +#endif + + Action a = () => result.Should().BeViewResult().WithViewData(key, expectedValue); + + a.Should() + .Throw() + .WithMessage(failureMessage) + .And.Should().NotBeOfType(); + } + [Test] public void WithViewData_GivenUnexpectedKey_ShouldFail() {