Modify FindAsyncMethodUsingAttribute to discover async test methods. #650
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes
FindMethodUsingAttributeso that it will correctly find the test method when the test method is async.If you use
ShouldMatchApprovedin an async test method, after an await, the continuation that is created causes the stack trace to loose information about the original calling method.e.g. This will fail with "Cannot find method in call stack with attribute NUnit.Framework.TestAttribute"
While investigating this problem I realized that the
TestMethodInfoclass already appears to be aware of this issue and includes code to resolve the async methods, however this has not been implemented inFindMethodUsingAttribute.To fix this, I've copied a number of classes/methods from
TestMethodInfoand applied them inFindMethodUsingAttribute. Note that I've copiedOriginalMethodInfo,GetOriginalMethodInfoForStateMachineMethodandContainsAttribute, and these are all unchanged, so it may be worth refactoring them to a shared location later.If anyone wishes to make use of this before the PR is merged, you can simply define a new
FindMethodUsingAttributewith a defferent name (e.g.FindAsyncMethodUsingAttribute, and then configureShouldMatchApprovedto use it like so...stringValue.ShouldMatchApproved(c => c.Configure(smc => smc.TestMethodFinder = new FindAsyncMethodUsingAttribute<TestAttribute>()));