简体   繁体   English

NUnit FileAssert在MemoryStream和FileStream之间失败

[英]NUnit FileAssert fails between MemoryStream and FileStream

I have a test class that tests two that a generated xml file matches one that is already generated and known to be good. 我有一个测试类,该类测试两个生成的xml文件与已经生成且已知良好的XML文件匹配的测试类。 The test passes when comparing files written to disk. 比较写入磁盘的文件时,测试通过。 It fails when comparing the file still in memory to the known good file read from the disk, because the expected stream is 17 bytes longer. 将仍在内存中的文件与从磁盘读取的已知正常文件进行比较时,它会失败,因为预期的流要长17个字节。

testing class 测试班

[TestFixture]
class XmlExporterTest
{
    private const string ExpectedXMLFile = @"..\..\Projects\EXPECTED FILE.xml";

    private XmlExporter xmlExporter;

    [SetUp]
    public void SetUp()
    {
        // clean up from before, since we might want the info after the tests (if they fail for example)
        string[] filePaths = Directory.GetFiles(@"..\..\tmp");
        foreach (string f in filePaths)
        {
            File.Delete(f);
        }
    }

    // this always fails because the stream length is 17 bytes short
    [Test]
    public void TestExportToXMLStream()
    {
        // test write to stream
        using (Stream actualStream = xmlExporter.ExportToXML(true))
        {
            using (Stream expectedStream = new FileStream(ExpectedXMLFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
            {
                FileAssert.AreEqual(expectedStream, actualStream);
            }
        }
    }

    // this always passes
    [Test]
    public void TestExportToXMLFile()
    {
        const string actualXMLFile = @"..\..\tmp\project1.xml";
        xmlExporter.ExportToXML(actualXMLFile, true);
        FileAssert.AreEqual(ExpectedXMLFile, actualXMLFile);
    }
}

XML exporting class XML导出类

public class XmlExporter
{
    public void ExportToXML(string filename, bool normalizeZoom)
    {
        iexINSPECTIONXPERT arr = CreateSerializableObject(normalizeZoom);

        //serialize to an XML file
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("xxx", "http://www.example.com");
        using (TextWriter tr2 = new StreamWriter(filename))
        {
            XmlSerializer sr2 = new XmlSerializer(typeof(Foo));
            sr2.Serialize(tr2, arr, ns);
        }
    }

    /// <summary>
    /// Exports to XML only returning a MemoryStream object.
    /// Note that the stream must be disposed of by the caller.
    /// </summary>
    /// <param name="normalizeZoom"></param>
    /// <returns></returns>
    public MemoryStream ExportToXML(bool normalizeZoom)
    {
        iexINSPECTIONXPERT arr = CreateSerializableObject(normalizeZoom);

        //serialize to an XML file
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("xxx", "http://www.example.com");
        var stream = new MemoryStream();
        XmlSerializer sr2 = new XmlSerializer(typeof(Foo));
        sr2.Serialize(stream, arr, ns);
        return stream;
    }
}

other class 其他班级

[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.example.com", ElementName = "Foo")]
public class Foo
{
    [XmlElementAttribute(Namespace = "", ElementName = "BAR")]
    public BAR fi;
}

You need to use the same encoding when writing to file and when writing to a memory stream, for example using Encoding.Default in both cases. 写入文件和写入内存流时,您需要使用相同的编码,例如在两种情况下都使用Encoding.Default

File: 文件:

using (TextWriter tr2 = new StreamWriter(filename, false, Encoding.Default))

Memory: 记忆:

var memory = new MemoryStream();
var writer = new StreamWriter(memory, Encoding.Default);
sr2.Serialize(writer, arr, ns);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM