xunit.net - 我可以将代表传递给 Xunit 理论吗
问题描述
我有兴趣在许多类上重用测试理论,特别是一些需要相同测试的构造函数。我最初的想法是使用委托来执行此功能。
但是,我认为我可能正在尝试重新发明轮子,尽管 C# 具有一些功能,但我认为我正在尝试一种不正确的方法。是否有使用比 InlineData 更正确的方法来支持此类事情的方法。
InlineData 似乎用于注入输入,因此我可能会测试给定测试的许多示例。但是我可以为几种方法提供几个变量并测试 ^x 而不是 *x
[Theory]
[InlineData(input => new ConcreteConstructor(input)) ]
public void Constructor_Should_Not_Throw_Expection (Action<string>)
{
constructor("SomeString");
}
注意我认为我应该Func
在这种情况下使用作为返回的对象。无论如何,我怀疑这完全是错误的方法,所以这不是主要考虑因素。
解决方案
public static IEnumerable<object[]> TestData()
{
Action<string> a1 = input => new ConcreteConstructor(input);
yield return new object[] { a1 };
Action<string> a2 = input => new AnotherConstructor(input);
yield return new object[] { a2 };
}
[Theory]
[MemberData(nameof(TestData))]
public void Constructor_Should_Not_Throw_Expection(Action<string> constructor)
{
constructor("SomeString");
}
推荐阅读
- apache-spark - Apache kyline 多维数据集流构建错误没有作业计数器
- tsql - 选择所有空值,但只有一个不为空
- c# - 对 ms-application-insights 的查询返回的结果与 PowerBI 查询不一致
- angular - 在Angular 7中查找数组中唯一对象的数量
- ios - 将 RxSwift 链接到命令行工具的正确方法
- javascript - 当我们将鼠标悬停在输入类型文本上时如何显示占位符
- spring - 在 Spring Data JPA 中加入(自定义)查询
- routing - 角度多级子路由
- javascript - 如何使用 jQuery 在更改时启用选择下拉菜单?
- php - BadMethodCallException:参数数量错误