TODO removal part 1 - ActionExecutorTests

This commit is contained in:
Ryan Nowak 2014-10-23 20:28:10 -07:00
parent 9a77c2bc1e
commit 46897037e9
1 changed files with 138 additions and 108 deletions

View File

@ -34,64 +34,85 @@ namespace Microsoft.AspNet.Mvc.Core.Test
[Fact] [Fact]
public async Task AsyncAction_WithVoidReturnType() public async Task AsyncAction_WithVoidReturnType()
{ {
// Arrange
var methodWithVoidReturnType = new MethodWithVoidReturnType(TestController.VoidAction); var methodWithVoidReturnType = new MethodWithVoidReturnType(TestController.VoidAction);
// Act
var result = await ControllerActionExecutor.ExecuteAsync( var result = await ControllerActionExecutor.ExecuteAsync(
methodWithVoidReturnType.GetMethodInfo(), methodWithVoidReturnType.GetMethodInfo(),
null, null,
(IDictionary<string, object>)null); (IDictionary<string, object>)null);
// Assert
Assert.Same(null, result); Assert.Same(null, result);
} }
[Fact] [Fact]
public async Task AsyncAction_TaskReturnType() public async Task AsyncAction_TaskReturnType()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskReturnType = new MethodWithTaskReturnType(_controller.TaskAction); var methodWithTaskReturnType = new MethodWithTaskReturnType(_controller.TaskAction);
// Act
var result = await ControllerActionExecutor.ExecuteAsync( var result = await ControllerActionExecutor.ExecuteAsync(
methodWithTaskReturnType.GetMethodInfo(), methodWithTaskReturnType.GetMethodInfo(),
_controller, _controller,
actionParameters); actionParameters);
// Assert
Assert.Same(null, result); Assert.Same(null, result);
} }
[Fact] [Fact]
public async Task AsyncAction_TaskOfValueReturnType() public async Task AsyncAction_TaskOfValueReturnType()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction);
// Act
var result = await ControllerActionExecutor.ExecuteAsync( var result = await ControllerActionExecutor.ExecuteAsync(
methodWithTaskOfIntReturnType.GetMethodInfo(), methodWithTaskOfIntReturnType.GetMethodInfo(),
_controller, _controller,
actionParameters); actionParameters);
// Assert
Assert.Equal(inputParam1, result); Assert.Equal(inputParam1, result);
} }
[Fact] [Fact]
public async Task AsyncAction_TaskOfTaskOfValueReturnType() public async Task AsyncAction_TaskOfTaskOfValueReturnType()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskOfTaskOfIntReturnType = new MethodWithTaskOfTaskOfIntReturnType(_controller.TaskOfTaskAction); var methodWithTaskOfTaskOfIntReturnType = new MethodWithTaskOfTaskOfIntReturnType(_controller.TaskOfTaskAction);
// Act
var result = await (Task<int>)(await ControllerActionExecutor.ExecuteAsync( var result = await (Task<int>)(await ControllerActionExecutor.ExecuteAsync(
methodWithTaskOfTaskOfIntReturnType.GetMethodInfo(), methodWithTaskOfTaskOfIntReturnType.GetMethodInfo(),
_controller, _controller,
actionParameters)); actionParameters));
// Assert
Assert.Equal(inputParam1, result); Assert.Equal(inputParam1, result);
} }
[Fact] [Fact]
public async Task AsyncAction_WithAsyncKeywordThrows() public async Task AsyncAction_WithAsyncKeywordThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionWithException); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionWithException);
@ -106,11 +127,14 @@ namespace Microsoft.AspNet.Mvc.Core.Test
[Fact] [Fact]
public async Task AsyncAction_WithoutAsyncThrows() public async Task AsyncAction_WithoutAsyncThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionWithExceptionWithoutAsync); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionWithExceptionWithoutAsync);
// Act & Assert
await Assert.ThrowsAsync<NotImplementedException>( await Assert.ThrowsAsync<NotImplementedException>(
() => ControllerActionExecutor.ExecuteAsync(methodWithTaskOfIntReturnType.GetMethodInfo(), () => ControllerActionExecutor.ExecuteAsync(methodWithTaskOfIntReturnType.GetMethodInfo(),
_controller, _controller,
@ -120,37 +144,48 @@ namespace Microsoft.AspNet.Mvc.Core.Test
[Fact] [Fact]
public async Task AsyncAction_WithExceptionsAfterAwait() public async Task AsyncAction_WithExceptionsAfterAwait()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionThrowAfterAwait); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionThrowAfterAwait);
await AssertThrowsAsync<ArgumentException>( var expectedException = "Argument Exception";
async () =>
await ControllerActionExecutor.ExecuteAsync( // Act & Assert
methodWithTaskOfIntReturnType.GetMethodInfo(), var ex = await Assert.ThrowsAsync<ArgumentException>(
_controller, () => ControllerActionExecutor.ExecuteAsync(
actionParameters), methodWithTaskOfIntReturnType.GetMethodInfo(),
"Argument Exception"); _controller,
actionParameters));
Assert.Equal(expectedException, ex.Message);
} }
[Fact] [Fact]
public async Task SyncAction() public async Task SyncAction()
{ {
string inputString = "hello"; // Arrange
var inputString = "hello";
var syncMethod = new SyncMethod(_controller.Echo); var syncMethod = new SyncMethod(_controller.Echo);
// Act
var result = await ControllerActionExecutor.ExecuteAsync( var result = await ControllerActionExecutor.ExecuteAsync(
syncMethod.GetMethodInfo(), syncMethod.GetMethodInfo(),
_controller, _controller,
new Dictionary<string, object>() { { "input", inputString } }); new Dictionary<string, object>() { { "input", inputString } });
// Assert
Assert.Equal(inputString, result); Assert.Equal(inputString, result);
} }
[Fact] [Fact]
public async Task SyncAction_WithException() public async Task SyncAction_WithException()
{ {
string inputString = "hello"; // Arrange
var inputString = "hello";
var syncMethod = new SyncMethod(_controller.EchoWithException); var syncMethod = new SyncMethod(_controller.EchoWithException);
// Act & Assert
await Assert.ThrowsAsync<NotImplementedException>( await Assert.ThrowsAsync<NotImplementedException>(
() => ControllerActionExecutor.ExecuteAsync( () => ControllerActionExecutor.ExecuteAsync(
syncMethod.GetMethodInfo(), syncMethod.GetMethodInfo(),
@ -161,151 +196,146 @@ namespace Microsoft.AspNet.Mvc.Core.Test
[Fact] [Fact]
public async Task AsyncAction_WithCustomTaskReturnTypeThrows() public async Task AsyncAction_WithCustomTaskReturnTypeThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
// If it is an unrecognized derived type we throw an InvalidOperationException. // If it is an unrecognized derived type we throw an InvalidOperationException.
var methodWithCutomTaskReturnType = new MethodWithCustomTaskReturnType(_controller.TaskActionWithCustomTaskReturnType); var methodWithCutomTaskReturnType = new MethodWithCustomTaskReturnType(_controller.TaskActionWithCustomTaskReturnType);
string expectedException = string.Format( var expectedException = string.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
"The method 'TaskActionWithCustomTaskReturnType' on type '{0}' returned a Task instance even though it is not an asynchronous method.", "The method 'TaskActionWithCustomTaskReturnType' on type '{0}' returned a Task instance even though it is not an asynchronous method.",
typeof(TestController)); typeof(TestController));
await AssertThrowsAsync<InvalidOperationException>(
async () => // Act & Assert
await ControllerActionExecutor.ExecuteAsync( var ex = await Assert.ThrowsAsync<InvalidOperationException>(
methodWithCutomTaskReturnType.GetMethodInfo(), () => ControllerActionExecutor.ExecuteAsync(
_controller, methodWithCutomTaskReturnType.GetMethodInfo(),
actionParameters), _controller,
expectedException); actionParameters));
Assert.Equal(expectedException, ex.Message);
} }
[Fact] [Fact]
public async Task AsyncAction_WithCustomTaskOfTReturnTypeThrows() public async Task AsyncAction_WithCustomTaskOfTReturnTypeThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithCutomTaskOfTReturnType = new MethodWithCustomTaskOfTReturnType(_controller.TaskActionWithCustomTaskOfTReturnType); var methodWithCutomTaskOfTReturnType = new MethodWithCustomTaskOfTReturnType(_controller.TaskActionWithCustomTaskOfTReturnType);
string expectedException = string.Format( var expectedException = string.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
"The method 'TaskActionWithCustomTaskOfTReturnType' on type '{0}' returned a Task instance even though it is not an asynchronous method.", "The method 'TaskActionWithCustomTaskOfTReturnType' on type '{0}' returned a Task instance even though it is not an asynchronous method.",
typeof(TestController)); typeof(TestController));
await AssertThrowsAsync<InvalidOperationException>( // Act & Assert
async () => var ex = await Assert.ThrowsAsync<InvalidOperationException>(
await ControllerActionExecutor.ExecuteAsync( () => ControllerActionExecutor.ExecuteAsync(
methodWithCutomTaskOfTReturnType.GetMethodInfo(), methodWithCutomTaskOfTReturnType.GetMethodInfo(),
_controller, _controller,
actionParameters), actionParameters));
expectedException); Assert.Equal(expectedException, ex.Message);
} }
[Fact] [Fact]
public async Task AsyncAction_ReturningUnwrappedTaskThrows() public async Task AsyncAction_ReturningUnwrappedTaskThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var methodWithUnwrappedTask = new MethodWithTaskReturnType(_controller.UnwrappedTask); var methodWithUnwrappedTask = new MethodWithTaskReturnType(_controller.UnwrappedTask);
await AssertThrowsAsync<InvalidOperationException>(
async () => var expectedException = string.Format(
await ControllerActionExecutor.ExecuteAsync( CultureInfo.CurrentCulture,
methodWithUnwrappedTask.GetMethodInfo(), "The method 'UnwrappedTask' on type '{0}' returned an instance of '{1}'. " +
_controller, "Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.",
actionParameters), typeof(TestController),
string.Format(CultureInfo.CurrentCulture, typeof(Task<Task>).FullName);
"The method 'UnwrappedTask' on type '{0}' returned an instance of '{1}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.",
typeof(TestController), // Act & Assert
typeof(Task<Task>).FullName var ex = await Assert.ThrowsAsync<InvalidOperationException>(
)); () => ControllerActionExecutor.ExecuteAsync(
methodWithUnwrappedTask.GetMethodInfo(),
_controller,
actionParameters));
Assert.Equal(expectedException, ex.Message);
} }
[Fact] [Fact]
public async Task AsyncAction_WithDynamicReturnTypeThrows() public async Task AsyncAction_WithDynamicReturnTypeThrows()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", inputParam1 }, { "s", inputParam2 } };
var dynamicTaskMethod = new ReturnTaskAsDynamicValue(_controller.ReturnTaskAsDynamicValue); var dynamicTaskMethod = new ReturnTaskAsDynamicValue(_controller.ReturnTaskAsDynamicValue);
string expectedException = string.Format( var expectedException = string.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
"The method 'ReturnTaskAsDynamicValue' on type '{0}' returned a Task instance even though it is not an asynchronous method.", "The method 'ReturnTaskAsDynamicValue' on type '{0}' returned a Task instance even though it is not an asynchronous method.",
typeof(TestController)); typeof(TestController));
await AssertThrowsAsync<InvalidOperationException>(
async () => // Act & Assert
await ControllerActionExecutor.ExecuteAsync( var ex = await Assert.ThrowsAsync<InvalidOperationException>(
dynamicTaskMethod.GetMethodInfo(), () => ControllerActionExecutor.ExecuteAsync(
_controller, dynamicTaskMethod.GetMethodInfo(),
actionParameters), _controller,
expectedException); actionParameters));
Assert.Equal(expectedException, ex.Message);
} }
[Fact] [Fact]
public async Task ParametersInRandomOrder() public async Task ParametersInRandomOrder()
{ {
int inputParam1 = 1; // Arrange
string inputParam2 = "Second Parameter"; var inputParam1 = 1;
var inputParam2 = "Second Parameter";
// Note that the order of parameters is reversed // Note that the order of parameters is reversed
var actionParameters = new Dictionary<string, object> { { "s", inputParam2 }, { "i", inputParam1 } }; var actionParameters = new Dictionary<string, object> { { "s", inputParam2 }, { "i", inputParam1 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction);
// Act
var result = await ControllerActionExecutor.ExecuteAsync( var result = await ControllerActionExecutor.ExecuteAsync(
methodWithTaskOfIntReturnType.GetMethodInfo(), methodWithTaskOfIntReturnType.GetMethodInfo(),
_controller, _controller,
actionParameters); actionParameters);
// Assert
Assert.Equal(inputParam1, result); Assert.Equal(inputParam1, result);
} }
[Fact] [Fact]
public async Task InvalidParameterValueThrows() public async Task InvalidParameterValueThrows()
{ {
string inputParam2 = "Second Parameter"; // Arrange
var inputParam2 = "Second Parameter";
var actionParameters = new Dictionary<string, object> { { "i", "Some Invalid Value" }, { "s", inputParam2 } }; var actionParameters = new Dictionary<string, object> { { "i", "Some Invalid Value" }, { "s", inputParam2 } };
var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction); var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction);
var message = TestPlatformHelper.IsMono ? "Object type {0} cannot be converted to target type: {1}" : var message = TestPlatformHelper.IsMono ? "Object type {0} cannot be converted to target type: {1}" :
"Object of type '{0}' cannot be converted to type '{1}'."; "Object of type '{0}' cannot be converted to type '{1}'.";
var expectedException = string.Format( var expectedException = string.Format(
CultureInfo.CurrentCulture, CultureInfo.CurrentCulture,
message, message,
typeof (string), typeof(string),
typeof (int)); typeof(int));
// Act & Assert
// If it is an unrecognized derived type we throw an InvalidOperationException. // If it is an unrecognized derived type we throw an InvalidOperationException.
await AssertThrowsAsync<ArgumentException>( var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => () => ControllerActionExecutor.ExecuteAsync(
await ControllerActionExecutor.ExecuteAsync( methodWithTaskOfIntReturnType.GetMethodInfo(),
methodWithTaskOfIntReturnType.GetMethodInfo(), _controller,
_controller, actionParameters));
actionParameters),
expectedException);
}
// TODO: XUnit Assert.Throw is not async-aware. Check if the latest version supports it. Assert.Equal(expectedException, ex.Message);
private static async Task AssertThrowsAsync<TException>(Func<Task<object>> func, string expectedExceptionMessage = "")
{
var expected = typeof(TException);
Type actual = null;
string actualExceptionMessage = string.Empty;
try
{
var result = await func();
}
catch (Exception e)
{
actual = e.GetType();
actualExceptionMessage = e.Message;
}
Assert.Equal(expected, actual);
Assert.Equal(expectedExceptionMessage, actualExceptionMessage);
} }
} }
} }