UnitTests(单元测试)在c#中实战
//文件:TestBase.cs using System; using System.IO; using System.Linq; using System.Reflection; using NiuX.Extensions; namespace UnitTests { public class TestBase { protected T CreateInstance<T>() { var json = File.ReadAllText($"Configs/{PatternConfigJsonPath}.json").ToJson<dynamic>(); return (T)Activator.CreateInstance(Assembly.Load(AssemblyName).GetTypes().First(x => x.FullName == (string)json.className)); } protected (T1, T2) CreateInstance<T1, T2>() { var json = File.ReadAllText($"Configs/{PatternConfigJsonPath}.json").ToJson<dynamic>(); var types = Assembly.Load(AssemblyName).GetTypes(); var v1 = json!.className[0]; var v2 = json.className[1]; return ((T1)Activator.CreateInstance(types.First(x => x.FullName == (string)v1)), (T2)Activator.CreateInstance(types.First(x => x.FullName == (string)v2))); } protected virtual string PatternConfigJsonPath { get; } protected virtual string AssemblyName { get; } } }
//文件:BuilderPatternTest.cs
using BuilderPattern.Sample1; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; namespace UnitTests { [TestClass] public class BuilderPatternTest : TestBase { protected override string PatternConfigJsonPath => "builderPattern"; protected override string AssemblyName => "BuilderPattern"; [TestMethod] public void Sample1() { var ac = new ActorController(); var obj = CreateInstance<ActionBuilder>(); var actor = ac.Construct(obj); Console.WriteLine($"{actor.GetType()} 的外观:"); Console.WriteLine($"性别:{actor.Sex}"); Console.WriteLine($"性别:{actor.Face}"); Console.WriteLine($"性别:{actor.Costume}"); Console.WriteLine($"性别:{actor.Hairstyle}"); } } }
3.建造者项目附附件中
如何应用到星空的二开中呢。
UnitTests(单元测试)在c#中实战
//文件:TestBase.csusing System;using System.IO;using System.Linq;using System.Reflection;using NiuX.Extensions;namespace UnitTests...
点击下载文档
本文2024-09-16 18:37:29发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-23351.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章