42

I heavily use the Autofixture AutoData Theories for creating my data and mocks. However this prevents me from using the InlineData Attributes from XUnit to pipe in a bunch of different data for my tests.

So I am basically looking for something like this:

[Theory, AutoMoqDataAttribute]
[InlineData(3,4)]
[InlineData(33,44)]
[InlineData(13,14)]
public void SomeUnitTest([Frozen]Mock<ISomeInterface> theInterface,  MySut sut, int DataFrom, int OtherData)
{
     // actual test omitted
}

Is something like this possible?

zlZimon
  • 2,334
  • 4
  • 21
  • 51
  • 2
    See also [AutoFixture, xUnit.net, and Auto Mocking](http://blog.nikosbaxevanis.com/2012/07/31/autofixture-xunit-net-and-auto-mocking/). – Nikos Baxevanis May 12 '16 at 06:52

2 Answers2

59

You'll have to create your own InlineAutoMoqDataAttribute, similar to this:

public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute
{
    public InlineAutoMoqDataAttribute(params object[] objects) : base(new AutoMoqDataAttribute(), objects) { }
}

and you'd use it like this:

[Theory]
[InlineAutoMoqData(3,4)]
[InlineAutoMoqData(33,44)]
[InlineAutoMoqData(13,14)]
public void SomeUnitTest(int DataFrom, int OtherData, [Frozen]Mock<ISomeInterface> theInterface, MySut sut)
{
     // actual test omitted
}

Note that the inlined data, the ints in this case, must be the first parameters of the test method. All the other parameters will be provided by AutoFixture.

Marcio Rinaldi
  • 3,305
  • 24
  • 23
  • See also http://blog.nikosbaxevanis.com/2011/08/25/combining-data-theories-in-autofixture-xunit-extension/ – ike Mar 08 '19 at 11:12
  • I got an exception when utilizing the above snippet. The following, instead, seems to be up to date with current Nunit features: `public InlineAutoMoqDataAttribute(params object[] objects) : base(() => new Fixture().Customize(new AutoMoqCustomization()), objects)` – Casper Bang Oct 18 '21 at 13:43
  • 1
    The answer is a bit outdated, since there is no class `AutoMoqDataAttribute` anymore. Up to date version is: `public InlineAutoMoqDataAttribute(params object[] objects) : base(new AutoDataAttribute(), objects) { }`. So, instead of `AutoMoqDataAttribute` `AutoDataAttribute` should be used. – Dmitriy Bobrovskiy Feb 22 '22 at 09:07
5

With the latest AutoFixture, you can use Inline AutoData Theories

Uses the InlineData values for the the first method arguments, and then uses AutoData for the rest (when the InlineData values run out).

bonzaster
  • 313
  • 2
  • 12