c# - 如何对 DataContractSerializer 进行单元测试?

标签 c# unit-testing testing

我创建了自己的序列化程序,代码如下:

public class BackgroundJobInfoSerializer : IBackgroundJobInfoSerializer
{
    private static readonly DataContractSerializer Serializer =
        new DataContractSerializer(typeof(BackgroundJobInfo),
                                   null,
                                   int.MaxValue,
                                   false,
                                   false,
                                   new MongoDbSurrogate());

    public string Serialize(BackgroundJobInfo info)
    {
        if (info == null)
        {
            throw new ArgumentNullException("info", BackgroundJobsLocalization.BackgroundJobInfoIsNull);
        }

        var stringBuilder = new StringBuilder();
        using (var stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
        {
            var writer = XmlWriter.Create(stringWriter);
            Serializer.WriteObject(writer, info);
            writer.Flush();
        }

        return stringBuilder.ToString();
    }

    public BackgroundJobInfo Deserialize(string info){...}

现在我想创建一个单元测试。但我想知道如何测试它?

我应该检查哪些常见测试用例以确保一切正常?

最佳答案

  1. 设置测试对象

  2. 序列化测试对象

  3. 重构为对象的新实例

  4. 验证测试对象和新对象匹配的所有数据点

关于c# - 如何对 DataContractSerializer 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940687/

相关文章:

c# - .net 4 xslt 扩展对象

c# - 使用unity拦截作为横切关注点解决异常处理

javascript - Karma vs 测试框架 Jasmine、Mocha、QUnit

unit-testing - 我如何使用 TestNG SkipException?

unit-testing - 如何测试setter和getter?

c# - 将正则表达式转换为 htmlagilitypack

c# - 在 C# WinForm 中计算# of Years Alive

.net - 在 TestCleanup 中传递参数

node.js - 使用 Sinon-Chai 时测试失败显示 "Error: timeout of 2000ms exceeded"

iOS UITests - 如何获取当前 View 中存在的 XCUIElement 列表