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

Cannot create FunctionFailedException in unit test #753

Closed
DarranShepherd opened this issue May 10, 2019 · 2 comments
Closed

Cannot create FunctionFailedException in unit test #753

DarranShepherd opened this issue May 10, 2019 · 2 comments
Assignees
Labels
Enhancement Feature requests. fix-ready Indicates that an issue has been fixed and will be available in the next release.
Milestone

Comments

@DarranShepherd
Copy link

I am creating unit tests for my orchestrator function, following the guidance on https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing .

I wish to test the logic that handles one of my activity tasks throwing an exception. The orchestrator handles this with

catch (FunctionFailedException ex) when (ex.InnerException is MyCustomException)

However, I am unable to mock the call to CallActivityAsync and have it throw as all constructors of FunctionFailedException are internal. As Exception.InnerException is not virtual, I also cannot use Mock.Of<FunctionFailedException>(ex => ex.InnerException == new MyCustomException())

I have been able to resolve this temporarily by using reflection, but I doubt this is the expected approach. Is this not something that should be tested for some reason, or is there an official way to do so?

private FunctionFailedException CreateException(string message, Exception inner)
{
    var ctor = typeof(FunctionFailedException).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(string), typeof(Exception) }, null);
    return (FunctionFailedException)ctor.Invoke(new object[] { message, inner });
}
@ghost ghost added the Needs: Triage 🔍 label May 10, 2019
@cgillum
Copy link
Member

cgillum commented May 10, 2019

I think it makes sense for us to expose those constructors to make things more unit testable. @ConnorMcMahon what do you think?

@cgillum cgillum added Enhancement Feature requests. and removed Needs: Triage 🔍 labels May 10, 2019
@cgillum cgillum added this to the v1.8.2 milestone May 10, 2019
@ConnorMcMahon ConnorMcMahon added the fix-ready Indicates that an issue has been fixed and will be available in the next release. label May 10, 2019
@cgillum
Copy link
Member

cgillum commented May 30, 2019

This fix was released as part of v1.8.2.

@cgillum cgillum closed this as completed May 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Feature requests. fix-ready Indicates that an issue has been fixed and will be available in the next release.
Projects
None yet
Development

No branches or pull requests

3 participants