UnitTests(单元测试)在c#中实战

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

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.建造者项目附附件中


image.webp

BuilderPattern.rar


如何应用到星空的二开中呢。

UnitTests(单元测试)在c#中实战

//文件:TestBase.csusing System;using System.IO;using System.Linq;using System.Reflection;using NiuX.Extensions;namespace UnitTests...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息