问题描述
在基于AutoFixture测试,我想尽可能干净表达了以下内容:
In an AutoFixture-based test, I'm trying to express as cleanly as possible the following:
的当我通过 <输入>
的参数 X
这种方法,在其他参数填充匿名,结果是... 的
When I pass <input>
to the parameter x
of this method, filling in the other parameters anonymously, the result is...
以某工厂方法的例子: -
Taking the example of a factory method:-
class X
{
public static X Create( Guid a, Guid b, Guid c, String x, String y);
我想表达一个简洁的一系列测试:
I'm trying to express as a succinct series of tests:
- 如果我传递null
X
,它应该抛出 - 如果我传递null
是
,它应该抛出
- If I pass null for
x
, it should throw - If I pass null for
y
, it should throw
为了表达我可以说:
var fixture = Fixture();
var sut = default( Func<Guid, Guid, Guid,string,X>);
sut = fixture.Get( ( Guid anonA, Guid anonB, Guid anonC, string anonY ) =>
x =>
X.Create( anonA, anonB, anonC, x, anonY ) );
Assert.Throws<ArgumentNullException>( () => sut( null));
有关第二种情况下,这是唯一略有不同,我需要做的:
For the second instance, which is only slightly different, I need to do:
var fixture = Fixture();
var sut = default( Func<Guid, Guid, Guid,string,X> );
sut = fixture.Get( ( Guid anonA, Guid anonB, Guid anonC, string anonX ) =>
y =>
X.Create( anonA, anonB, anonC, anonX, y ) );
Assert.Throws<ArgumentNullException>( () => sut( null));
有关的属性,还有在
在AutoFixture。是否有方法(和/或构造函数)参数的eqivalent?
For properties, there's With
in AutoFixture. Is there an eqivalent for method (and/or ctor) arguments ?
PS 0我不如果有必要进入这种情况下魔力字符串介意 - 即具有 X
位×
PS 0. I don't mind if its necessary to get into 'magic' strings for this case - i.e., having the x
bit be "x"
.
PS 1.房间里的大象等是我撞我的头靠在获取
在AutoFixture的4倍过载 - 或者是因为我有一个旧?版本在这种环境下
PS 1. The other elephant in the room is that I'm bumping my head against the 4x overloads of Get
in AutoFixture - or is that because I have an old version in this environment?
PS 2,也能更好的建议开放为如何建模这一点 - 只要他们处理的事实,我希望它是一个方法调用,而不是属性或字段(我想它在AutoFixture stylee工作)。
PS 2. Also open to better suggestions as to how to model this - as long as they deal with the fact that I want it to be a method invocation and not properties or fields (and I'd like it to work in an AutoFixture stylee).
推荐答案
有真正的ISN '吨AutoFixture任何功能,使得这更容易,但我开放的建议。不过,我看不出你如何能表达一个强类型的方式类似的东西。将语法是什么样子的?
There really isn't any feature in AutoFixture that makes this easier, but I'm open to suggestions. However, I don't see how you could express something like that in a strongly typed fashion. What would the syntax look like?
不过,如果您只需要进行测试的空逆天的工作,你可以使用AutoFixture.Idioms为。
However, if you only need this for testing that Null Guards work, you can use AutoFixture.Idioms for that.
下面是一个例子。
var fixture = new Fixture();
var assertion = new GuardClauseAssertion(fixture);
var method = typeof(GuardedMethodHost).GetMethod("ConsumeStringAndInt32AndGuid");
assertion.Verify(method);
如果你看的,你会发现其他的例子,但我承认它是AutoFixture的更差记录区之一...
If you look at the source code of Ploeh.AutoFixture.IdiomsUnitTest.Scenario
you'll find other examples, but I admit it's one of the more poorly documented areas of AutoFixture...
另一件事完全是很少(或没有)的参数的方法是优于与许多参数的方法,让你有没有考虑引入参数对象?
Another thing entirely is that methods with few (or no) parameters are better than methods with many parameters, so have you considered Introducing a Parameter Object?
这篇关于广义函数功能的包装用匿名值相当于AutoFixture“与”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!