質問

When I try to use approvals with my unit test decorated with [Theory] attribute it says:

System.Exception: System.Exception : Approvals is not set up to use your test framework.
It currently supports [NUnit, MsTest, MbUnit, xUnit.net]
To add one use ApprovalTests.StackTraceParsers.StackTraceParser.AddParser() method to add implementation of ApprovalTests.StackTraceParsers.IStackTraceParser with support for your testing framework.
To learn how to implement one see http://blog.approvaltests.com/2012/01/creating-namers.html
   at ApprovalTests.StackTraceParsers.StackTraceParser.Parse(StackTrace stackTrace)
   at ApprovalTests.Namers.UnitTestFrameworkNamer..ctor()
   at ApprovalTests.Approvals.GetDefaultNamer()
   at ApprovalTests.Approvals.Verify(IApprovalWriter writer)
   at ApprovalTests.Approvals.Verify(Object text)

Seems like it recognizes [Fact] attributes only. I tried to follow the link from stacktrace but there is nothing about how to plug in your own namer/parser into approvals.

Is there any entry point where I can add my own namer/parser? Itself it seems to be trivial, the only question is how to use it:

 public class TheoryNamer : AttributeStackTraceParser
    {
        protected override string GetAttributeType()
        {
            return typeof(TheoryAttribute).FullName;
        }

        public override string ForTestingFramework
        {
            get { return "xUnit Extensions"; }
        }
    }
役に立ちましたか?

解決

There are a couple parts to this answer & question.

  1. How to add
  2. The Namer
  3. Dealing with data driven tests in approvaltests

1) How to add

Adding is simple (if a bit rough) The method mentioned should have been static, but it works none the less.

To add one use ApprovalTests.StackTraceParsers.StackTraceParser.AddParser() method to add implementation of ApprovalTests.StackTraceParsers.IStackTraceParser with support for your testing framework.

so you'll need to do a

new StackTraceParser().AddParser(new TheoryNamer());

I apologize for this, and it will be static in the next version (v.21)

2) The Namer

The Namer is suppose to generate a unique name for each approved/received file. This is normally done on the name of the method, however the name here will not be unique as a theory based test will be data driven and therefore have multiple calls to the same method.

Naming:  classname.methodname(optional: .additionalInformation).received.extension

As such, you will probably have to include additional information in the method it's self

public class StringTests1
{
    [Theory,
    InlineData("goodnight moon"),
    InlineData("hello world")]
    public void Contains(string input)
    {
        NamerFactory.AdditionalInformation = input;  // <- important
        Approvals.Verify(transform(input));
    }
} 

3) Dealing with data driven tests in approvaltests

To be honest, in most cases, the data driven method of approach in Approval Tests isn't thru parameters in the Method Decorators. It is usually thru the VerifyAll with a lambda transform. 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()));
}

Which would create the received file:

goodnight moon => GOODNIGHT MOON
hello world => HELLO WORLD

他のヒント

It's better to inherit TheoryNamer class from XUnitStackTraceParser.
It works perfect!
I think it would be cool to add such class into ApprovalTests.StackTraceParsers namespace :)

public class XUnitTheoryStackTraceParser : XUnitStackTraceParser
{
    public const string TheoryAttribute = "Xunit.Extensions.TheoryAttribute";

    protected override string GetAttributeType()
    {
        return TheoryAttribute;
    }
}

public class ApproveTheoryTest
{
    static ApproveTheoryTest()
    {
        StackTraceParser.AddParser(new XUnitTheoryStackTraceParser());
    }

    [Theory]
    [UseReporter(typeof(DiffReporter))]
    [InlineData("file1.txt")]
    [InlineData("file2.txt")]
    public void approve_file(string fileName)
    {
        NamerFactory.AdditionalInformation = fileName;
        Approvals.Verify("sample text");
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top