Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically approve approval-tests when I run them?

I'm using ApprovalTests.Net. I see that I can specify various reports. I would like to automatically approve the tests when I run the unit tests. I need to do this only temporarily, or when the code goes through major changes. This is especially useful when creating data-driven ApprovalTests using ApprovalResults.ForScenario.

Is there a way to do that?

like image 496
zumalifeguard Avatar asked Jun 03 '16 00:06

zumalifeguard


1 Answers

Create an reporter but rather than implementing IApprovalFailureReporter, implement IReporterWithApprovalPower. IReporterWithApprovalPower has an additional method, ApprovedWhenReported where you can do the work of approving the test.

Here's an example reporter that will automatically copy the received file to the approved file:

// Install-package ApprovalTests 
// Install-package ObjectApproval
public class AutoApprover : IReporterWithApprovalPower
{
    public static readonly AutoApprover INSTANCE = new AutoApprover();

    private string approved;
    private string received;

    public void Report(string approved, string received)
    {
        this.approved = approved;
        this.received = received;
        Trace.WriteLine(string.Format(@"Will auto-copy ""{0}"" to ""{1}""", received, approved));
    }

    public bool ApprovedWhenReported()
    {
        if (!File.Exists(this.received)) return false;
        File.Delete(this.approved);
        if (File.Exists(this.approved)) return false;
        File.Copy(this.received, this.approved);
        if (!File.Exists(this.approved)) return false;

        return true;
    }
}

You can then use it in your test class or method the way you specify any approver:

[UseReporter(typeof(AutoApprover))] 
like image 68
zumalifeguard Avatar answered Oct 04 '22 04:10

zumalifeguard