我需要檢查是否只有一種回傳“ExpamleType”的類方法(“ExampleMethod”)。我可以用 C# 中的 ArchUnit 來做到這一點嗎?
uj5u.com熱心網友回復:
您不需要使用 ArchUnit - 或者其他任何東西,只需使用 .NET 的內置反射 API:
using System;
using System.Linq;
using System.Reflection;
#if !( XUNIT || MSTEST || NUNIT)
#error "Specify unit testing framework"
#endif
#if MSTEST
[TestClass]
#elif NUNIT
[TestFixture]
#endif
public class ReflectionTests
{
#if XUNIT
[Fact]
#elif MSTEST
[TestMethod]
#elif NUNIT
[Test]
#endif
public void MyClass_should_have_only_1_ExampleMethod()
{
const BindingFlags BF = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
MethodInfo mi = typeof(MyClass).GetMethods( BF ).Single( m => m.Name == "ExampleMethod" );
#if XUNIT
Assert.Equal( expected: typeof(ExampleType), actual: mi.ReturnType );
#elif MSTEST || NUNIT
Assert.AreEqual( expected: typeof(ExampleType), actual: mi.ReturnType );
#endif
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340222.html
上一篇:使用Python3.10默認使用Tkinter深色主題?
下一篇:每次運行測驗時如何增加int
