There are a couple of parts to this answer and question.
- How to add
- The namer
- Working with data-based validations in approval tests
1) How to add
Adding is simple (if a little rough) The specified method should be static, but it works no less.
To add one use, the ApprovalTests.StackTraceParsers.StackTraceParser.AddParser () method to add an ApprovalTests.StackTraceParsers.IStackTraceParser implementation that supports your test environment.
so you need to do
new StackTraceParser().AddParser(new TheoryNamer());
I apologize for this, and it will be static in the next version (v.21)
2) Namer
Namer is supposed to create a unique name for each approved / accepted file. This is usually done by the name of the method, but the name here will not be unique, since a theory-based test will be data driven and therefore has several calls to the same method.
Naming: classname.methodname(optional: .additionalInformation).received.extension
Thus, you probably have to include additional information in the it self method.
public class StringTests1 { [Theory, InlineData("goodnight moon"), InlineData("hello world")] public void Contains(string input) { NamerFactory.AdditionalInformation = input;
3) Work with data-based validations in approval tests
Honestly, in most cases, the data-based approach method in approval tests is independent of the parameters in Decorators. This usually happens through VerifyAll with lambda conversion. For example, the above might look like
[Fact] public void UpperCase() { var inputs = new[]{"goodnight moon","hello world"}; Approvals.VerifyAll(inputs, i => "{0} => {1}".FormatWith(i, i.ToUpperInvariant())); }
To create the resulting file:
goodnight moon => GOODNIGHT MOON hello world => HELLO WORLD