【发布时间】:2014-07-17 07:45:40
【问题描述】:
简明扼要:
class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
public interface IWeapon { bool LaunchAtEarth(double probabilityOfHitting); }
public class DeathStar
{
readonly IWeapon _weapon;
public DeathStar(IWeapon weapon) // guard clause omitted for brevity
{
this._weapon = weapon;
}
public bool FireFromOrbit(double probabilityOfHitting)
{
return this._weapon.LaunchAtEarth(probabilityOfHitting);
}
}
// Make me pass, pretty please with a cherry on the top
[Test, AutoMoqData]
[TestCase(0.1), TestCase(0.5), TestCase(1)]
public void AutoMoqData_should_fill_rest_of_arguments_that_are_not_filled_by_TestCase(
double probabilityOfHitting,
[Frozen] Mock<IWeapon> weapon,
DeathStar platform)
{
Assert.That(weapon, Is.Not.Null);
Assert.That(platform, Is.Not.Null);
} // Ignored with error: Wrong number of parameters specified.
// Make me pass, too!
[Test, AutoMoqData]
[TestCaseSource("GetTestCases")]
public void AutoMoqData_should_fill_rest_method_arguments_that_are_not_filled_by_TestCaseSource(
double probabilityOfHitting,
[Frozen] Mock<IWeapon> weapon,
DeathStar platform)
{
Assert.That(weapon, Is.Not.Null);
Assert.That(platform, Is.Not.Null);
} // Ignored with error: Wrong number of parameters specified.
IEnumerable<TestCaseData> GetTestCases()
{
yield return new TestCaseData(0.1);
yield return new TestCaseData(0.5);
yield return new TestCaseData(1);
}
如果我使用 Xunit,这似乎可以解决问题: http://nikosbaxevanis.com/blog/2011/08/25/combining-data-theories-in-autofixture-dot-xunit-extension/ 我找不到任何与 NUnit 等效的东西。
这个:http://gertjvr.wordpress.com/2013/08/29/my-first-open-source-contribution/ 似乎已经在当前版本的 AutoFixture.NUnit2 (AutoData 属性)中工作,但是当我想让它采用 params object[] 以便填充测试方法中的第一个参数时,它不能处理这种情况使用属性参数,其余的传递给 AutoData 属性。
有人可以引导我走向正确的方向吗?好像缺少了什么我无法掌握的东西。
【问题讨论】:
-
AutoFixture.xUnit.net 和 AutoFixture.NUnit2 具有相似的类型名称,但它们建立在不同的对象模型上。 – 看起来你需要一个类似于InlineAutoDataAttribute 的
TestCaseAutoDataAttribute。 -
这正是我想要的,但找不到。
标签: c# unit-testing testing nunit autofixture