Skip to content
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

Update code to use C# 6 features #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ _ReSharper*
*/NhibernateProfiler*/tools
packages
/TestStack.FluentMVCTesting.Example/App_Data
.vs/config/applicationhost.config
.vs/TestStack.FluentMVCTesting/v15/sqlite3/storage.ide
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<Name>TestStack.FluentMVCTesting.Sample</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions TestStack.FluentMVCTesting.Tests/AsyncControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void Work_correctly_for_invalid_check()
() => _controller.WithCallTo(c => c.AsyncViewAction()).ShouldGiveHttpStatus()
);
}

[Test]
public void Work_correctly_for_valid_child_action_check()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class ControllerExtensionsShould
private ControllerExtensionsController _controller;

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

[Test]
public void Give_controller_modelstate_errors()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ partial class ControllerResultTestShould
private ControllerResultTestController _controller;

[SetUp]
public void Setup()
{
_controller = new ControllerResultTestController();
}
public void Setup() => _controller = new ControllerResultTestController();

#region General Tests
// Expected action return types for the different types of assertions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Emit_readable_error_message_when_the_actual_content_encoding_has_not
{
var exception = Assert.Throws<ActionResultAssertionException>(() => _controller.WithCallTo(c => c.ContentWithoutEncodingSpecified()).ShouldReturnContent(encoding: ControllerResultTestController.TextualContentEncoding));

Assert.That(exception.Message, Is.EqualTo(string.Format("Expected encoding to be equal to {0}, but instead was null.", ControllerResultTestController.TextualContentEncoding.EncodingName)));
Assert.That(exception.Message, Is.EqualTo($"Expected encoding to be equal to {ControllerResultTestController.TextualContentEncoding.EncodingName}, but instead was null."));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>6</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -108,7 +109,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMVCTesting.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="NSubstitute" version="1.7.2.0" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="NUnitTestAdapter" version="2.1.1" targetFramework="net45" />
</packages>
22 changes: 7 additions & 15 deletions TestStack.FluentMvcTesting/ControllerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace TestStack.FluentMVCTesting
{
public static class ControllerExtensions
{

public static T WithModelErrors<T>(this T controller) where T : Controller
{
controller.ModelState.AddModelError("Key", "Value");
Expand Down Expand Up @@ -43,7 +42,7 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
var action = ((MethodCallExpression)actionCall.Body).Method;

if (!action.IsDefined(typeof(ChildActionOnlyAttribute), false))
throw new InvalidControllerActionException(string.Format("Expected action {0} of controller {1} to be a child action, but it didn't have the ChildActionOnly attribute.", action.Name, controller.GetType().Name));
throw new InvalidControllerActionException($"Expected action {action.Name} of controller {controller.GetType().Name} to be a child action, but it didn't have the ChildActionOnly attribute.");

return controller.WithCallTo(actionCall);
}
Expand All @@ -55,7 +54,7 @@ public static ControllerResultTest<T> WithCallToChild<T, TAction>(this T control
var action = ((MethodCallExpression)actionCall.Body).Method;

if (!action.IsDefined(typeof(ChildActionOnlyAttribute), false))
throw new InvalidControllerActionException(string.Format("Expected action {0} of controller {1} to be a child action, but it didn't have the ChildActionOnly attribute.", action.Name, controller.GetType().Name));
throw new InvalidControllerActionException($"Expected action {action.Name} of controller {controller.GetType().Name} to be a child action, but it didn't have the ChildActionOnly attribute.");

return controller.WithCallTo(actionCall);
}
Expand All @@ -66,22 +65,17 @@ public static TempDataResultTest ShouldHaveTempDataProperty(this ControllerBase

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

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));
throw new TempDataAssertionException($"Expected value to be of type {value.GetType().FullName}, but instead was {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));
throw new TempDataAssertionException($"Expected value for key '{key}' to be '{value}', but instead found '{actual}'");
}

return new TempDataResultTest(controller);
Expand All @@ -93,8 +87,7 @@ public static TempDataResultTest ShouldHaveTempDataProperty<TValue>(this Control

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

if (!predicate((TValue)actual))
Expand All @@ -111,8 +104,7 @@ public static TempDataResultTest ShouldNotHaveTempDataProperty(this ControllerBa

if (actual != null)
{
throw new TempDataAssertionException(string.Format(
"Expected TempData to have no value with key '{0}', but found one.", key));
throw new TempDataAssertionException($"Expected TempData to have no value with key '{key}', but found one.");
}

return new TempDataResultTest(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ namespace TestStack.FluentMVCTesting
{
public partial class ControllerResultTest<T> where T : Controller
{
public T Controller { get; private set; }
public string ActionName { get; private set; }
public ActionResult ActionResult { get; private set; }
public T Controller { get; }
public string ActionName { get; }
public ActionResult ActionResult { get; }

public void ValidateActionReturnType<TActionResult>() where TActionResult : ActionResult
{
var castedActionResult = ActionResult as TActionResult;

if (ActionResult == null)
throw new ActionResultAssertionException(string.Format("Received null action result when expecting {0}.", typeof(TActionResult).Name));
throw new ActionResultAssertionException($"Received null action result when expecting {typeof(TActionResult).Name}.");

if (castedActionResult == null)
throw new ActionResultAssertionException(
string.Format("Expected action result to be a {0}, but instead received a {1}.",
typeof(TActionResult).Name, ActionResult.GetType().Name
)
);
throw new ActionResultAssertionException($"Expected action result to be a {typeof(TActionResult).Name}, but instead received a {ActionResult.GetType().Name}.");
}

public ControllerResultTest(T controller, string actionName, ActionResult actionResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class ControllerResultTest<T>
public HttpStatusCodeResult ShouldGiveHttpStatus()
{
ValidateActionReturnType<HttpStatusCodeResult>();
return (HttpStatusCodeResult) ActionResult;
return (HttpStatusCodeResult)ActionResult;
}

public HttpStatusCodeResult ShouldGiveHttpStatus(int status)
Expand All @@ -18,8 +18,8 @@ public HttpStatusCodeResult ShouldGiveHttpStatus(int status)
var statusCodeResult = (HttpStatusCodeResult)ActionResult;

if (statusCodeResult.StatusCode != status)
throw new ActionResultAssertionException(string.Format("Expected HTTP status code to be '{0}', but instead received a '{1}'.", status, statusCodeResult.StatusCode));
return (HttpStatusCodeResult) ActionResult;
throw new ActionResultAssertionException($"Expected HTTP status code to be '{status}', but instead received a '{statusCodeResult.StatusCode}'.");
return (HttpStatusCodeResult)ActionResult;
}

public HttpStatusCodeResult ShouldGiveHttpStatus(HttpStatusCode status)
Expand Down
57 changes: 20 additions & 37 deletions TestStack.FluentMvcTesting/ControllerResultTest/ShouldRedirectTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ShouldRedirectTo(string url)
var redirectResult = (RedirectResult)ActionResult;

if (redirectResult.Url != url)
throw new ActionResultAssertionException(string.Format("Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'.", url, redirectResult.Url));
throw new ActionResultAssertionException($"Expected redirect to URL '{url}', but instead was given a redirect to URL '{redirectResult.Url}'.");
}

public RouteValueDictionary ShouldRedirectToRoute(string route)
Expand All @@ -24,35 +24,25 @@ public RouteValueDictionary ShouldRedirectToRoute(string route)
var redirectResult = (RedirectToRouteResult)ActionResult;

if (redirectResult.RouteName != route)
throw new ActionResultAssertionException(string.Format("Expected redirect to route '{0}', but instead was given a redirect to route '{1}'.", route, redirectResult.RouteName));
throw new ActionResultAssertionException($"Expected redirect to route '{route}', but instead was given a redirect to route '{redirectResult.RouteName}'.");

return redirectResult.RouteValues;
}

public RouteValueDictionary ShouldRedirectTo(Func<T, Func<ActionResult>> actionRedirectedTo)
{
return ShouldRedirectTo(actionRedirectedTo(Controller).Method);
}
public RouteValueDictionary ShouldRedirectTo(Func<T, Func<ActionResult>> actionRedirectedTo) =>
ShouldRedirectTo(actionRedirectedTo(Controller).Method);

public RouteValueDictionary ShouldRedirectTo(Func<T, Func<int, ActionResult>> actionRedirectedTo)
{
return ShouldRedirectTo(actionRedirectedTo(Controller).Method);
}
public RouteValueDictionary ShouldRedirectTo(Func<T, Func<int, ActionResult>> actionRedirectedTo) =>
ShouldRedirectTo(actionRedirectedTo(Controller).Method);

public RouteValueDictionary ShouldRedirectTo<T1>(Func<T, Func<T1, ActionResult>> actionRedirectedTo)
{
return ShouldRedirectTo(actionRedirectedTo(Controller).Method);
}
public RouteValueDictionary ShouldRedirectTo<T1>(Func<T, Func<T1, ActionResult>> actionRedirectedTo) =>
ShouldRedirectTo(actionRedirectedTo(Controller).Method);

public RouteValueDictionary ShouldRedirectTo<T1, T2>(Func<T, Func<T1, T2, ActionResult>> actionRedirectedTo)
{
return ShouldRedirectTo(actionRedirectedTo(Controller).Method);
}
public RouteValueDictionary ShouldRedirectTo<T1, T2>(Func<T, Func<T1, T2, ActionResult>> actionRedirectedTo) =>
ShouldRedirectTo(actionRedirectedTo(Controller).Method);

public RouteValueDictionary ShouldRedirectTo<T1, T2, T3>(Func<T, Func<T1, T2, T3, ActionResult>> actionRedirectedTo)
{
return ShouldRedirectTo(actionRedirectedTo(Controller).Method);
}
public RouteValueDictionary ShouldRedirectTo<T1, T2, T3>(Func<T, Func<T1, T2, T3, ActionResult>> actionRedirectedTo) =>
ShouldRedirectTo(actionRedirectedTo(Controller).Method);

public RouteValueDictionary ShouldRedirectTo(Expression<Action<T>> actionRedirectedTo)
{
Expand All @@ -69,13 +59,13 @@ public RouteValueDictionary ShouldRedirectTo(MethodInfo method, RouteValueDictio
var redirectResult = (RedirectToRouteResult)ActionResult;

if (redirectResult.RouteValues.ContainsKey("Controller") && redirectResult.RouteValues["Controller"].ToString() != controllerName)
throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"]));
throw new ActionResultAssertionException($"Expected redirect to controller '{controllerName}', but instead was given a redirect to controller '{redirectResult.RouteValues["Controller"]}'.");

if (!redirectResult.RouteValues.ContainsKey("Action"))
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect without an action.", actionName));
throw new ActionResultAssertionException($"Expected redirect to action '{actionName}', but instead was given a redirect without an action.");

if (redirectResult.RouteValues["Action"].ToString() != actionName)
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"]));
throw new ActionResultAssertionException($"Expected redirect to action '{actionName}', but instead was given a redirect to action '{redirectResult.RouteValues["Action"]}'.");

if (expectedValues == null)
return redirectResult.RouteValues;
Expand All @@ -85,16 +75,11 @@ public RouteValueDictionary ShouldRedirectTo(MethodInfo method, RouteValueDictio
object actualValue;
if (!redirectResult.RouteValues.TryGetValue(expectedRouteValue.Key, out actualValue))
{
throw new ActionResultAssertionException(string.Format("Expected redirect to have parameter '{0}', but it did not.", expectedRouteValue.Key));
throw new ActionResultAssertionException($"Expected redirect to have parameter '{expectedRouteValue.Key}', but it did not.");
}
if (actualValue.ToString() != expectedRouteValue.Value.ToString())
{
throw new ActionResultAssertionException(
string.Format("Expected parameter '{0}' to have value '{1}', but instead was given value '{2}'."
, expectedRouteValue.Key
, expectedRouteValue.Value
, actualValue
));
throw new ActionResultAssertionException($"Expected parameter '{expectedRouteValue.Key}' to have value '{expectedRouteValue.Value}', but instead was given value '{actualValue}'.");
}
}

Expand Down Expand Up @@ -122,15 +107,13 @@ public RouteValueDictionary ShouldRedirectTo<TController>(MethodInfo methodInfo)
}

if (redirectResult.RouteValues["Controller"] == null)
{
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}' in '{1}' controller, but instead was given redirect to action '{2}' within the same controller.", actionName, controllerName, redirectResult.RouteValues["Action"]));
}
throw new ActionResultAssertionException($"Expected redirect to action '{actionName}' in '{controllerName}' controller, but instead was given redirect to action '{redirectResult.RouteValues["Action"]}' within the same controller.");

if (redirectResult.RouteValues["Controller"].ToString() != controllerName)
throw new ActionResultAssertionException(string.Format("Expected redirect to controller '{0}', but instead was given a redirect to controller '{1}'.", controllerName, redirectResult.RouteValues["Controller"]));
throw new ActionResultAssertionException($"Expected redirect to controller '{controllerName}', but instead was given a redirect to controller '{redirectResult.RouteValues["Controller"]}'.");

if (redirectResult.RouteValues["Action"].ToString() != actionName)
throw new ActionResultAssertionException(string.Format("Expected redirect to action '{0}', but instead was given a redirect to action '{1}'.", actionName, redirectResult.RouteValues["Action"]));
throw new ActionResultAssertionException($"Expected redirect to action '{actionName}', but instead was given a redirect to action '{redirectResult.RouteValues["Action"]}'.");

return redirectResult.RouteValues;
}
Expand Down
Loading