public void WhenInvokeMethodContextHasResultSetAndThenExceptionItShouldHaveExceptionAndNoResult() { // Given var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !; var parameters = new object[0]; var context = new InvokeMethodContext( method, parameters, this); var exception = new Exception(); var result = new object(); // When context.SetResult(result); context.SetException(exception); // Then context.HasResult.Should().BeFalse(); context.Result.Should().BeNull(); context.HasException.Should().BeTrue(); context.Exception.Should().BeSameAs(exception); }
public void WhenInvokeMethodContextIsNotSetTypedReturnShouldFail() { // Given var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !; var parameters = new object[0]; var context = new InvokeMethodContext( method, parameters, this); // Then Action action = () => context.Return <int>(); action.Should() .Throw <InvokeMethodWithoutResultException>(); }
public static async Task CallAsyncFunc(InvokeMethodContext context, CancellationToken cancellationToken) { try { var task = (Task)context.Method.Invoke( obj: context.Instance, parameters: context.Parameters) !; await task; var taskType = task.GetType(); if (taskType.IsGenericType && taskType.GetGenericTypeDefinition() == typeof(Task <>)) { var property = TaskResultPropertyCache .GetOrAdd(taskType, t => taskType.GetProperty( name: nameof(Task <string> .Result), bindingAttr: BindingFlags.Public | BindingFlags.Instance, binder: default,
public static void CallSyncFunc(InvokeMethodContext context) { try { var result = context.Method.Invoke( obj: context.Instance, parameters: context.Parameters); context.SetResult(result); } catch (TargetInvocationException exception) { context.SetException(exception.InnerException ?? exception); } catch (Exception exception) { context.SetException(exception); } }
public void WhenInvokeMethodContextHasResultTypedReturnShouldNotFail() { // Given var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !; var parameters = new object[0]; var context = new InvokeMethodContext( method, parameters, this); var result = 42; // When context.SetResult(result); // Then context.Return <int>().Should().Be(42); }
public void WhenInvokeMethodContextIsCreatedItShouldNotHaveResult() { // Given var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !; var parameters = new object[0]; var context = new InvokeMethodContext( method, parameters, this); // Then context.Method.Should().BeSameAs(method); context.Parameters.Should().BeSameAs(parameters); context.Instance.Should().BeSameAs(this); context.HasResult.Should().BeFalse(); context.Result.Should().BeNull(); context.HasException.Should().BeFalse(); context.Exception.Should().BeNull(); }
public void WhenInvokeMethodContextHasExceptionTypedReturnShouldFail() { // Given var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !; var parameters = new object[0]; var context = new InvokeMethodContext( method, parameters, this); var exception = new FormatException(); // When context.SetException(exception); // Then Action action = () => context.Return <int>(); action.Should() .Throw <TargetInvocationException>() .WithInnerException <FormatException>(); }