Skip to content

Support for checking for temp data values. #33

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 10 commits into from
Sep 14, 2014
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
<Compile Include="..\TestStack.FluentMvcTesting\ModelTest.cs">
<Link>ModelTest.cs</Link>
</Compile>
<Compile Include="..\TestStack.FluentMvcTesting\TempDataResultTest.cs">
<Link>TempDataResultTest.cs</Link>
</Compile>
<Compile Include="..\TestStack.FluentMvcTesting\ViewResultTest.cs">
<Link>ViewResultTest.cs</Link>
</Compile>
Expand Down
107 changes: 106 additions & 1 deletion TestStack.FluentMVCTesting.Tests/ControllerExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.IO;
using NUnit.Framework;
using TestStack.FluentMVCTesting.Tests.TestControllers;

namespace TestStack.FluentMVCTesting.Tests
Expand Down Expand Up @@ -41,5 +42,109 @@ public void Throw_exception_for_child_action_call_to_non_child_action()
var exception = Assert.Throws<InvalidControllerActionException>(() => _controller.WithCallToChild(c => c.SomeAction()));
Assert.That(exception.Message, Is.EqualTo("Expected action SomeAction of controller ControllerExtensionsController to be a child action, but it didn't have the ChildActionOnly attribute."));
}

[Test]
public void Check_for_existent_temp_data_property()
{
const string key = "";
_controller.TempData[key] = "";

_controller.ShouldHaveTempDataProperty(key);
}

[Test]
public void Check_for_non_existent_temp_data_property()
{
const string key = "";

var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key));

Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value()
{
const string key = "";
const int value = 10;
_controller.TempData[key] = value;

_controller.ShouldHaveTempDataProperty(key, value);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value()
{
const string key = "";
const int actualValue = 0;
const int expectedValue = 1;
_controller.TempData[key] = actualValue;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key, expectedValue));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types()
{
const string key = "";
const int actualValue = 0;
const string expectedValue = "one";
_controller.TempData[key] = actualValue;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key, expectedValue));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value to be of type {0}, but instead was {1}.", expectedValue.GetType().FullName, actualValue.GetType().FullName)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality()
{
const string key = "";
MemoryStream expectedValue = new MemoryStream();
_controller.TempData[key] = expectedValue;

_controller.ShouldHaveTempDataProperty(key, expectedValue);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate()
{
const string key = "";
const int value = 1;
_controller.TempData[key] = value;

_controller
.ShouldHaveTempDataProperty<int>(key, x => x == value);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate()
{
const string key = "";
_controller.TempData[key] = 1;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));

Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
}

[Test]
public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate()
{
const string key = "";

var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));

Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
}
}
}
123 changes: 123 additions & 0 deletions TestStack.FluentMVCTesting.Tests/TempDataResultTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.IO;
using NUnit.Framework;
using TestStack.FluentMVCTesting.Tests.TestControllers;

namespace TestStack.FluentMVCTesting.Tests
{
[TestFixture]
public class TempDataResultTestShould
{
private ControllerExtensionsController _controller;
private TempDataResultTest _tempDataTest;

[SetUp]
public void Setup()
{
_controller = new ControllerExtensionsController();
_tempDataTest = new TempDataResultTest(_controller);
}

[Test]
public void Check_for_existent_temp_data_property()
{
const string key = "";
_controller.TempData[key] = "";

_tempDataTest.AndShouldHaveTempDataProperty(key);
}

[Test]
public void Check_for_non_existent_temp_data_property()
{
const string key = "";

var exception = Assert.Throws<TempDataAssertionException>(() =>
_tempDataTest.AndShouldHaveTempDataProperty(key));

Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value()
{
const string key = "";
const int value = 10;
_controller.TempData[key] = value;

_tempDataTest.AndShouldHaveTempDataProperty(key, value);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value()
{
const string key = "";
const int actualValue = 0;
const int expectedValue = 1;
_controller.TempData[key] = actualValue;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, expectedValue, actualValue)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types()
{
const string key = "";
const int actualValue = 0;
const string expectedValue = "one";
_controller.TempData[key] = actualValue;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value to be of type {0}, but instead was {1}.", expectedValue.GetType().FullName, actualValue.GetType().FullName)));
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality()
{
const string key = "";
MemoryStream expectedValue = new MemoryStream();
_controller.TempData[key] = expectedValue;

_tempDataTest.AndShouldHaveTempDataProperty(key, expectedValue);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate()
{
const string key = "";
const int value = 1;
_controller.TempData[key] = value;

_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == value);
}

[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate()
{
const string key = "";
_controller.TempData[key] = 1;

var exception = Assert.Throws<TempDataAssertionException>(() =>
_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == 0));

Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
}

[Test]
public void Check_for_non_existent_temp_data_property_when_supplied_with_predicate()
{
const string key = "";

var exception = Assert.Throws<TempDataAssertionException>(() =>
_tempDataTest.AndShouldHaveTempDataProperty<int>(key, x => x == 0));

Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<ItemGroup>
<Compile Include="AsyncControllerTests.cs" />
<Compile Include="RouteValueDictionaryExtensionsTests.cs" />
<Compile Include="TempDataResultTest.cs" />
<Compile Include="TestControllers\AsyncController.cs" />
<Compile Include="ControllerExtensionsTests.cs" />
<Compile Include="ControllerResultTestTests.cs" />
Expand Down Expand Up @@ -106,4 +107,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
45 changes: 45 additions & 0 deletions TestStack.FluentMvcTesting/ControllerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,50 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control

return controller.WithCallTo(actionCall);
}

public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase controller, string key, object value = null)
{
var actual = controller.TempData[key];

if (actual == null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
}

if (value != null && actual.GetType() != value.GetType())
{
throw new TempDataAssertionException(string.Format(
"Expected value to be of type {0}, but instead was {1}.",
value.GetType().FullName,
actual.GetType().FullName));
}

if (value != null && !value.Equals(actual))
{
throw new TempDataAssertionException(string.Format(
"Expected value for key \"{0}\" to be \"{1}\", but instead found \"{2}\"", key, value, actual));
}

return new TempDataResultTest(controller);
}

public static TempDataResultTest ShouldHaveTempDataProperty<TValue>(this ControllerBase controller, string key, Func<TValue, bool> predicate)
{
var actual = controller.TempData[key];

if (actual == null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have a non-null value with key \"{0}\", but none found.", key));
}

if (!predicate((TValue)actual))
{
throw new TempDataAssertionException("Expected view model to pass the given condition, but it failed.");
}

return new TempDataResultTest(controller);
}
}
}
5 changes: 5 additions & 0 deletions TestStack.FluentMvcTesting/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace TestStack.FluentMVCTesting
{

public class TempDataAssertionException : Exception
{
public TempDataAssertionException(string message) : base(message) { }
}

public class ActionResultAssertionException : Exception
{
public ActionResultAssertionException(string message) : base(message) { }
Expand Down
27 changes: 27 additions & 0 deletions TestStack.FluentMvcTesting/TempDataResultTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Web.Mvc;

namespace TestStack.FluentMVCTesting
{
public class TempDataResultTest
{
private readonly ControllerBase _controller;

public TempDataResultTest(ControllerBase controller)
{
_controller = controller;
}

public TempDataResultTest AndShouldHaveTempDataProperty(string key, object value = null)
{
_controller.ShouldHaveTempDataProperty(key, value);
return this;
}

public TempDataResultTest AndShouldHaveTempDataProperty<TValue>(string key, Func<TValue, bool> predicate)
{
_controller.ShouldHaveTempDataProperty(key, predicate);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="ModelErrorTest.cs" />
<Compile Include="ModelTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TempDataResultTest.cs" />
<Compile Include="ViewResultTest.cs" />
</ItemGroup>
<ItemGroup>
Expand Down