【问题标题】:NUnit FileAssert fails between MemoryStream and FileStreamNUnit FileAssert 在 MemoryStream 和 FileStream 之间失败
【发布时间】:2012-07-25 15:32:50
【问题描述】:

我有一个测试类,它测试两个生成的 xml 文件与一个已经生成并且已知是好的文件匹配。比较写入磁盘的文件时测试通过。将仍在内存中的文件与从磁盘读取的已知良好文件进行比较时失败,因为预期的流长了 17 个字节。

测试类

[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 导出类

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;
    }
}

其他类

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

【问题讨论】:

    标签: c# xml serialization stream nunit


    【解决方案1】:

    在写入文件和写入内存流时需要使用相同的编码,例如在这两种情况下都使用Encoding.Default

    文件:

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

    内存:

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

    【讨论】:

    • 我会认为编码是相同的,因为我在同一台计算机上运行所有这些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多