gpt4 book ai didi

xunit.net - 我可以将委托(delegate)传递给 Xunit 理论吗

转载 作者:行者123 更新时间:2023-12-05 01:33:36 27 4
gpt4 key购买 nike

我有兴趣在多个类上重用测试理论,特别是一些需要相同测试的构造函数。我最初有使用委托(delegate)来执行此功能的想法。

但是,我认为我可能正在尝试重新发明轮子,尽管 C# 具有一些功能,但我认为我正在尝试一种不正确的方法。对于这种使用比 InlineData 更正确的方法,是否有支持的方法。

InlineData 似乎用于注入(inject)输入,因此我可能会测试给定测试的许多示例。但是我可以为几种方法提供几个变量并进行测试 ^x 而不是 *x

[Theory]
[InlineData(input => new ConcreteConstructor(input)) ]
public void Constructor_Should_Not_Throw_Expection (Action<string>)
{
constructor("SomeString");
}

N.B 我认为在这种情况下我应该使用 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");
}

关于xunit.net - 我可以将委托(delegate)传递给 Xunit 理论吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64679421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com