Catel:反序列化适用于 1 个单元测试,但对另一个单元测试失败

Catel: Deserialize works for 1 unit test, fails for another

使用 VS 2015。

我正在将我的应用程序从 Catel 4.4.0 升级到 4.5.4。当然,使用 SavableModelBase.Load 参数对模型进行了大量更改,但我修复了这些,并且所有内容都可以编译,没有错误或警告。

我 运行 我的单元测试,我有 1 个测试失败了。该测试需要委托人加载先前序列化的 XML 数据文件。然后委托按年份从集合中检索一个 YearConstants 实例。代表是:

public static YearConstants GetYearConstantsByYearAndFileName(int year, string fileName)
    {
        using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            var constants = ConstantsData.Load(fs, SerializationMode.Xml, null);
            var results = constants.YearConstantsList.Where(x => x.Year == year).FirstOrDefault();
            if (results == null)
            {
                throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "YearConstants not found for [{0}] in file [{1}]", year, fileName));
            }

            return results;
        }
    }

当使用此委托的单元测试为 运行 时,"constants" 变量有一个值,但不包含任何年份数据。当我中断所有 CLR 异常时,出现以下异常:

System.InvalidCastException occurred
  HResult=-2147467262
  Message=Unable to cast object of type 'Catel.Runtime.Serialization.Xml.XmlSerializationContextInfo' to type 'Catel.Runtime.Serialization.Binary.BinarySerializationContextInfo'.
  Source=Catel.Core
  StackTrace:
       at Catel.Runtime.Serialization.SerializerBase`1.Deserialize(Object model, ISerializationContextInfo serializationContext, ISerializationConfiguration configuration) in C:\CI_WS\Ws969\Source\Catel\src\Catel.Core\Catel.Core.Shared\Runtime\Serialization\SerializerBase.deserialization.cs:line 154
  InnerException: 

我已经添加了一个单元测试来测试加载同一个文件,它无一例外地通过了:

    [TestMethod]
    [DeploymentItem(@"TestData\GeorgiaConstants.xml")]
    public void ConstantsData_SerializationWithLiveDataTest()
    {
        using (var fs = new FileStream("GeorgiaConstants.xml", FileMode.Open, FileAccess.Read))
        {
            var constants = ConstantsData.Load(fs, SerializationMode.Xml, null);
            Assert.IsNotNull(constants, "The constants data was not correctly loaded from 'GeorgiaConstants.xml' file.");
            Assert.AreEqual(5, constants.YearConstantsList.Count, "The expected number of year constants was not loaded from the 'GeorgiaConstants.xml' file.");

            // We should have year constants for 2016 for a while in the file.
            var yearConstants = constants.YearConstantsList.Where(x => x.Year == 2016).FirstOrDefault();
            Assert.IsNotNull(yearConstants, "The year constants for year = 2016 were not found in 'GeorgiaConstants.xml' file.");

            // We shouldn't be able to find year constants for a year this old.
            yearConstants = constants.YearConstantsList.Where(x => x.Year == 1999).FirstOrDefault();
            Assert.IsNull(yearConstants, "The year constants for year = 1999 were UNEXPECTEDLY found in 'GeorgiaConstants.xml' file.");
        }
    }

两个文件流显示读取的字节数相同。此外,常量 XML 文件包含一个 "created" 日期、"modified" 日期和 "created by" 字符串,它们被正确读取。如果我 运行 失败的单元测试本身,它仍然会失败,所以它似乎不是文件 sharing/locking 问题。只是年份常量的 ObservableCollection 被第二个代码正确读取,而不是第一个。

我 运行 没有什么可以尝试的了。有没有人对我可以使用的这种或另一种方法的原因有任何建议?

谢谢, 运行dy

它看起来像是混淆了 Xml 和二进制序列化方法。请在 official issue tracker 中创建一个工单,以便解决此问题。