Comments (6)
So given a compiled awaited method it should definitely be possible to figure out the original method name - that is after all something that ILSpy does - but looking at the StackTrace in the debugger I couldn't find any clear link between the stackframe and the original method.
So here's an idea.
The relevant stackframe that should be picked up as containing the name is something like
{ApprovalTests.Xunit.<FullAsyncTest>d__8.MoveNext()}
So in theory it is absolutely possible to parse out the original method name (FullAsyncTest). This generated method is compiled into a nested class that is inside the original class.
Therefore, you're in a stackframe that matches the above pattern, you can navigate to the original containing class, and try to get the method that matches the generic parameter name. If this method is async its probably a safe bet that this was the original method and we can use it for naming.
Roundabout and I haven't tried it, but it seems like it should work...
from approvaltests.net.
Anyone ever find a workaround for this?
from approvaltests.net.
@mattscully the workaround that I used was to just feed in a test name manually
from approvaltests.net.
public class ConstantNamer : IApprovalNamer
{
public string SourcePath { get { return defaultNamer.SourcePath; } }
public string Name { get; private set; }
public ConstantNamer(string name) {
Name = Path.GetInvalidPathChars().Aggregate(name, (path, c) => path.Replace(c, ' ')).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Join(" ");
}
readonly IApprovalNamer defaultNamer = Approvals.GetDefaultNamer();
static ConstantNamer() {
if (!StackTraceParser.GetParsers().OfType<XbehaveStackTraceParser>().Any())
StackTraceParser.AddParser(new XbehaveStackTraceParser());
}
public class XbehaveStackTraceParser : AttributeStackTraceParser
{
public override bool Parse(StackTrace trace) {
base.Parse(trace);
//This should work but for some reason doesn't http://stackoverflow.com/questions/26553231/why-can-i-not-find-a-custom-attribute-on-this-methodinfo
//this.approvalFrame = caller.NonLambdaCallers.FirstOrDefault(c => c.Method.GetCustomAttributes(typeof (SpecificationAttribute), true).Any());
//instead this is a little fragile but should work.
this.approvalFrame = caller.NonLambdaCallers.Last(c => c.Class.Namespace.Split('.').Contains("AppleHms"));
return this.approvalFrame != null;
}
public override string ForTestingFramework {
get { return "xUnit.net"; }
}
protected override string GetAttributeType() {
return typeof(ScenarioAttribute).FullName;
}
}
}
use like this:
Approvals.Verify(
new ApprovalTextWriter(JsonConvert.SerializeObject(obj, Formatting.Indented), "json"),
new ConstantNamer(testName), new MultiReporter(new FileLauncherReporter(), new PowerShellClipboardReporter())
)
from approvaltests.net.
@togakangaroo Thanks! I'll give this a try. Is the AppleHms namespace specific to your application or is that something that is generic?
from approvaltests.net.
@mattscully whoopsie - that's my app. The point is that I'm walking the stacktrace to find the highest level stack frame that is still in my app (and in my app-specific namespace)
from approvaltests.net.
Related Issues (20)
- These files have different encodings. Left file: Unicode (UTF-8) with signature. Right file: Central European (Windows). You can resolve the difference by saving the right file with the encoding Unicode (UTF-8) with signature. HOT 7
- Set project to not conflict with NCrunch HOT 2
- VisualStudioReporter incorrect comparison order of approved and received? HOT 6
- Upgrade DiffEngine to enable Left/Right diff behavior
- System.Exception : Could not find a diff tool for extension: .txt HOT 1
- VerifyPdfFile failed at the random generated ID HOT 7
- Looking for VS 2022 support HOT 6
- Any documentation how to use ApprovalTests with Specflow? HOT 2
- 404 Errors from dead links in help document HOT 1
- `UseApprovalSubdirectory` does not work on derived classes HOT 1
- Verifying log testing... HOT 1
- The compare appear to be in wrong order HOT 4
- DiffEngine.ProcessCleanup throws System.TypeInitializationException when running failing test HOT 1
- Update to DiffEngine 11
- drop net461 support
- NUNIT TestCase doesn't work HOT 1
- Next version release date? HOT 2
- Verify using xUnit fails HOT 1
- DiffEngine 15.2.0
- EmptyFiles 8.1.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from approvaltests.net.