5

Given the following test:

[Theory]
[PropertyData("GetValidInputForDb")]
public void GivenValidInputShouldOutputCorrectResult(
    string patientId
    , string patientFirstName
)
{
    var fixture = new Fixture();          

    var sut = fixture.Create<HtmlOutputBuilder>();

    sut.DoSomething();
    // More code
}

I want to encapsulate fixture creation in its own class, something akin to:

[Theory]
[CustomPropertyData("GetValidInputForDb")]
public void GivenValidInputShouldOutputCorrectResult(
    string patientId
    , string patientFirstName
    , HtmlOutputBuilder sut
)
{
    sut.DoSomething();
    // More code
}

The problem is that I'm using PropertyData and the latter is supplying two input parameters. The fact that I'm then trying to automatically create my fixture as a parameter is causing an exception.

Here is the CustomPropertyData:

public class CustomPropertyDataAttribute : CompositeDataAttribute
{
    public CustomPropertyDataAttribute(string validInput)
        :base(new DataAttribute[]
            {
                new PropertyDataAttribute(validInput),
                new AutoDataAttribute(new Fixture()
                    .Customize(new HtmlOutpuBuilderTestConvention() )), 
            })
    {

    }
}

What are the options to resolve this?

DavidS
  • 2,179
  • 4
  • 26
  • 44

1 Answers1

3

You need to supply data to the PropertyDataAttribute as below:

public static IEnumerable<object[]> GetValidInputForDb 
{
    get
    {
        yield return new object[]
        {
            "123", 
            "abc"
        };
    }
}

The patientId value will be 123, the patientFirstName value will be abc and the SUT value is going to be supplied automatically by AutoFixture.

The CustomPropertyDataAttribute looks good.

Nikos Baxevanis
  • 10,868
  • 2
  • 46
  • 80
  • Ok let me go back to the drawing board because I pretty much did what you've suggested. However, I did get an exception. I'll try it again and try to gist it if need be. Thanks for your help. – DavidS May 30 '13 at 19:33
  • It looks like it's either something wrong with the `HtmlOutpuBuilderTestConvention` or perhaps a question targeting the `PropertyDataAttribute` at xUnit.net forums.. To test it replace `HtmlOutputBuilder` with `IDisposable` and `HtmlOutpuBuilderTestConvention` with `AutoMoqCustomization` you can see that the test executes. – Nikos Baxevanis May 31 '13 at 11:46
  • Thanks for the advice. I've still got to try you suggestion but I'll get back to you once I've figured out what's wrong. – DavidS May 31 '13 at 12:54
  • Your answer is correct as the issue I was having is related to xUnit rather than AutoFixture. – DavidS Jun 03 '13 at 08:01