Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 认可测试及PDF_C#_Testing_Approval Tests - Fatal编程技术网

C# 认可测试及PDF

C# 认可测试及PDF,c#,testing,approval-tests,C#,Testing,Approval Tests,我可以将ApprovalTests与PDF一起使用吗?我试着使用FileLauncher,但看起来相同的PDF在文件(位)级别略有不同。还是我用错了 [TestMethod] [UseReporter(typeof(FileLauncherReporter))] public void TestPdf() { var createSomePdf = PdfCreate(); ApprovalTests.Approvals.Verify(new FileInfo(createSo

我可以将ApprovalTests与PDF一起使用吗?我试着使用FileLauncher,但看起来相同的PDF在文件(位)级别略有不同。还是我用错了

[TestMethod]
[UseReporter(typeof(FileLauncherReporter))]
public void TestPdf()
{
    var createSomePdf = PdfCreate();

    ApprovalTests.Approvals.Verify(new FileInfo(createSomePdf.FileName));

}

Pdf很可能是用时间戳创建的。根据用于创建pdf的方法,您可能能够模拟创建的时间。但我不得不擦洗它

这是我用来做这件事的代码

    public static void VerifyPdf(string coverFile)
    {
        ScrubPdf(coverFile);
        Approvals.Verify(new ExistingFileWriter(coverFile));
    }

    private static void ScrubPdf(string coverFile)
    {
        long location;
        using (var pdf = File.OpenRead(coverFile))
        {
            location = Find("/CreationDate (", pdf);

        }
        using (var pdf = File.OpenWrite(coverFile))
        {
            pdf.Seek(location, SeekOrigin.Begin);

            var original = "/CreationDate (D:20110426104115-07'00')";
            var desired = new System.Text.ASCIIEncoding().GetBytes(original);

            pdf.Write(desired, 0, desired.Length);
            pdf.Flush();
        }
    }

我找到了一个命令行工具。比较2个PDF,如果它们相同,则返回退出代码0,如果它们不同,则返回1。下载+提取+添加到您的路径

缺点-它必须呈现两个PDF以执行差异。如果它们很大,则性能较高

审批人(主要基于
ApprovalTests.Approvers.FileApprover
):

要验证:

DiffPdfApprover.Verify(pdfBytes)

diff pdf也可以直观地显示差异。我为此找了个记者,但没怎么用。我认为,如果在最初的报告开发(我现在就在这里)之后出现回归,它将派上用场


看起来这是ApprovalTests内置的

用法:

Approvals.VerifyPdfFile(pdfFileLocation);
见:


谢谢Llewellyn,我的pdf生成器(itextsharp)创建了一个修改过的日期和文档id,我也不得不添加它们。我是ApprovalTests的新手,从我所看到的文档非常稀少(尤其是pdf)。您能解释一下如何编写验证PDF的单元测试吗?github上的示例使它看起来像是一个文件名。received.pdf应该已经创建,但我找不到。@Ben看一下项目中的。但你是对的,它似乎没有为PDF创建.received文件。这是我自己在测试中创造的。谢谢@Mathew。现在它至少运行了比较,但仍然失败。如果我自己比较这两个文档,那么创建日期是不同的(我想这是由PdfScrubber.ScrubPdf(…)方法处理的),并且文件底部附近还有两个不同的guid
public class DiffPdfReporter : GenericDiffReporter
{
    private static readonly string Path = FindFullPath("diff-pdf.exe");
    public DiffPdfReporter() : base(Path,
        GetArgs(),
        "Please put diff-pdf.exe in your %PATH%. https://github.com/vslavik/diff-pdf. And restart whatever's running the tests. Everything seems to cache the %PATH%.") { }

    private static string GetArgs()
    {
        return "--view \"{0}\" \"{1}\"";
    }

    private static string FindFullPath(string programInPath)
    {
        foreach (var path in from path in Environment.GetEnvironmentVariable("path").Split(';')
                             select path)
        {
            var fullPath = System.IO.Path.Combine(path, programInPath);
            if (File.Exists(fullPath))
                return fullPath;
        }
        return null;
    }
}
Approvals.VerifyPdfFile(pdfFileLocation);
public static void VerifyPdfFile(string pdfFilePath)
{
    PdfScrubber.ScrubPdf(pdfFilePath);
    Verify(new ExistingFileWriter(pdfFilePath));
}