c# - 如何在 ArchUnit c# 中检查方法返回类型
问题描述
我需要检查是否只有一个类方法(“ExampleMethod”)返回“ExampleType”。我可以用 C# 中的 ArchUnit 做到这一点吗?
解决方案
您不需要使用 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
}
}
推荐阅读
- javascript - 带有 .ico 扩展名的 Img 在 Chrome 上失真,而在 Firefox 上运行良好
- javascript - 页面滚动显示
- ios - 如果将 IPA 提交给客户端,是否可以获得崩溃日志?
- php - Laravel PHP 查询“在哪里不确定 id”
- python - 连续显示两个pyglet窗口
- qt - Qt:无法使用父背景图像为 qwidget 设置背景颜色
- javascript - Three.js:使用点、InstancedBufferGeometry 和 RawShaderMaterial 进行光线投射(GPU 拾取)
- android - 全局捕获异常并更改活动
- roc - 使用 ROC 值作为混淆矩阵中的阈值
- python - jupyter notebook 中的 XGBoost 内核崩溃