Skip to content

Conversation

@Kermittt
Copy link
Contributor

@Kermittt Kermittt commented Aug 3, 2020

This PR fixes FindMethodUsingAttribute so that it will correctly find the test method when the test method is async.

If you use ShouldMatchApproved in 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"

        [Test]
        public async Task ShouldMatchSnapshot_AsyncTestWithTestAttribute_ShouldMatchStringValueSnapshot()
        {
            await Task.Delay(1);

            const string stringValue = "String Value";
            stringValue.ShouldMatchApproved(c => c.LocateTestMethodUsingAttribute<TestAttribute>());
        }

While investigating this problem I realized that the TestMethodInfo class already appears to be aware of this issue and includes code to resolve the async methods, however this has not been implemented in FindMethodUsingAttribute.

To fix this, I've copied a number of classes/methods from TestMethodInfo and applied them in FindMethodUsingAttribute. Note that I've copied OriginalMethodInfo, GetOriginalMethodInfoForStateMachineMethod and ContainsAttribute, 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 FindMethodUsingAttribute with a defferent name (e.g. FindAsyncMethodUsingAttribute, and then configure ShouldMatchApproved to use it like so...
stringValue.ShouldMatchApproved(c => c.Configure(smc => smc.TestMethodFinder = new FindAsyncMethodUsingAttribute<TestAttribute>()));

@SimonCropp
Copy link
Contributor

thanks. can you add that failing test to the PR?

@Kermittt
Copy link
Contributor Author

Kermittt commented Aug 3, 2020

thanks. can you add that failing test to the PR?

No problem. Have done so.

@SimonCropp
Copy link
Contributor

thanks 👍

@SimonCropp SimonCropp merged commit 9844801 into shouldly:master Aug 4, 2020
@Kermittt Kermittt deleted the FindAsyncMethodUsingAttributeAsync branch August 4, 2020 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants