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

[WiP] Make sure ActionInvoker throws with InnerException #1727

Merged
merged 4 commits into from
Dec 21, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/Scaffolding/VS.Web.CG.Core/ActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ internal void BuildCommandLine(CommandLineApplication command)
ex = ex.GetBaseException();
}

throw new InvalidOperationException(ex.Message);
throw new InvalidOperationException(ex.Message, ex);
}

return 0;
};
}
}
}
}
37 changes: 36 additions & 1 deletion test/Scaffolding/VS.Web.CG.Core.Test/ActionInvokerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ public void ActionInvoker_Invokes_GenerateCode_Method_With_Right_ActionModel()
Assert.True(invokedModel.BoolProperty);
}

[Fact]
public void ActionInvoker_Throws_With_Inner_Exception()
{
//Arrange
const string NOT_IMPLEMENTED_MESSAGE = "This action is intentionally not implemented.";
var codeGenInstance = new CodeGeneratorSample((model) =>
{
throw new NotImplementedException(NOT_IMPLEMENTED_MESSAGE);
});

var serviceProviderMock = new Mock<IServiceProvider>();
var generatorMock = new Mock<CodeGeneratorDescriptor>(typeof(CodeGeneratorSample).GetTypeInfo(),
serviceProviderMock.Object);

generatorMock
.SetupGet(cd => cd.CodeGeneratorInstance)
.Returns(codeGenInstance);
generatorMock
.SetupGet(cd => cd.Name)
.Returns(typeof(CodeGeneratorSample).Name);

var actionDescriptor = new ActionDescriptor(generatorMock.Object,
typeof(CodeGeneratorSample).GetMethod("GenerateCode")); //This is not a perfect unit test as the arrange is using actual instance rather than a mock

var actionInvoker = new ActionInvoker(actionDescriptor);

//Act
var ex =
Assert.Throws<InvalidOperationException>(
() => actionInvoker.Execute("CodeGeneratorSample StringValuePassed --BoolProperty".Split(' ')));

//Assert
Assert.Equal(NOT_IMPLEMENTED_MESSAGE, ex.InnerException.Message);
}

private class CodeGeneratorSample
{
private Action<CodeGeneratorModel> _generateCodeImpl;
Expand Down Expand Up @@ -85,4 +120,4 @@ public string NonWritableProperty
public int IntProperty { get; set; }
}
}
}
}