You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

测试Execute方法时,如何配置SimpleParseRecord中的deserializedObj.entries?

如何在测试用例中配置deserializedObj的entries属性

要测试SimpleParseRecord方法,核心是让它能接收到带有有效entries属性的deserializedObj实例,以下是两种实用方案:


方案1:通过公共方法ProcessFile触发(推荐,符合黑盒测试逻辑)

SimpleParseRecord由私有方法SimpleParse间接调用,而SimpleParse又由公共方法ProcessFile触发。可以通过模拟IParserFile的行为,让它返回构造好的JSON内容,反序列化后自然生成带有entries的deserializedObj。

具体步骤:

  1. 构造符合JSON结构的测试数据
    根据Json.BasicSystemInfo.RootObject、Entry、Content的结构,生成包含entries的JSON字符串。示例:

    {
      "entries": [
        {
          "content": {
            "systemName": "TestPC",
            "cpuCores": 8
          }
        }
      ]
    }
    
  2. 模拟IParserFile接口
    用测试框架(如Moq)模拟IParserFile,让它返回包含上述JSON的字节流,同时配置必要的属性:

    // 构造测试JSON的字节流
    var testJson = "{\"entries\":[{\"content\":{\"systemName\":\"TestPC\",\"cpuCores\":8}}]}";
    var stream = new MemoryStream(Encoding.UTF8.GetBytes(testJson));
    
    // 模拟IParserFile实例
    var mockFile = new Mock<IParserFile>();
    mockFile.Setup(f => f.Open()).Returns(stream);
    mockFile.Setup(f => f.FileName).Returns("basicsysteminfo.json");
    mockFile.Setup(f => f.SizeInBytes).Returns(testJson.Length);
    mockFile.Setup(f => f.Guid).Returns(Guid.NewGuid());
    mockFile.Setup(f => f.Log).Returns(new Mock<ILog>().Object);
    
    // 调用公共方法触发整个流程
    var processor = new YourProcessorClass(_mockDbContext.Object);
    processor.ProcessFile(mockFile.Object);
    

    此时SimpleParse会将JSON反序列化为Json.BasicSystemInfo.RootObject,其entries属性就是你定义的测试数据,最终传入SimpleParseRecord。


方案2:直接反射调用私有方法SimpleParseRecord(白盒测试)

如果需要单独验证SimpleParseRecord的逻辑,可以通过反射直接调用它,手动构造deserializedObj并设置entries属性。

具体步骤:

  1. 构造R类型实例并赋值entries
    以Json.BasicSystemInfo.RootObject为例,直接创建实例并初始化entries列表:

    // 构造测试用的Content和Entry
    var testContent = new Json.BasicSystemInfo.Content { SystemName = "TestPC", CpuCores = 8 };
    var testEntry = new Json.BasicSystemInfo.Entry { Content = testContent };
    // 构造RootObject并设置entries
    var deserializedObj = new Json.BasicSystemInfo.RootObject();
    deserializedObj.entries = new List<Json.BasicSystemInfo.Entry> { testEntry };
    
  2. 反射调用私有方法
    获取私有方法的元信息并执行调用:

    var processor = new YourProcessorClass(_mockDbContext.Object);
    // 获取泛型私有方法
    var method = typeof(YourProcessorClass)
        .GetMethod("SimpleParseRecord", BindingFlags.NonPublic | BindingFlags.Instance)
        .MakeGenericMethod(
            typeof(BasicSystemInfo),
            typeof(Json.BasicSystemInfo.RootObject),
            typeof(Json.BasicSystemInfo.Entry),
            typeof(Json.BasicSystemInfo.Content)
        );
    // 传入参数并调用
    method.Invoke(processor, new object[] { deserializedObj, mockFile.Object });
    

关键注意点

  • 确保Json.Root<E>基类或子类已定义公共/内部的entries属性(代码中直接访问deserializedObj.entries,必须保证该属性可访问)。
  • 测试时需模拟_context.GetTable<T>().Insert的行为,避免操作真实数据库。

内容的提问来源于stack exchange,提问作者siva prasad reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:45:31