InlineAutoDataAttribute but for NUnit (for TestCase and TestCaseSource)

admin

Administrator
Staff member
To be succint:

Code:
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);
}

This seems to do the trick if I were using Xunit:
<a href="http://nikosbaxevanis.com/blog/2011...-theories-in-autofixture-dot-xunit-extension/" rel="noreferrer">http://nikosbaxevanis.com/blog/2011...-theories-in-autofixture-dot-xunit-extension/</a>
I cannot find anything equivalent for NUnit.

This: <a href="http://gertjvr.wordpress.com/2013/08/29/my-first-open-source-contribution/" rel="noreferrer">http://gertjvr.wordpress.com/2013/08/29/my-first-open-source-contribution/</a>
seems to be already working in the current version of AutoFixture.NUnit2 (the AutoData attribute), but it doesn't handle the case when I want to make it take a params object[] such that the first parameters in the test method are filled using the attribute arguments and the rest are passed through to the AutoData attribute.

Can someone guide me towards the right direction ? There seems to be something missing I cannot grasp.