AutoFixture: PropertyData and heterogeneous parameters

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 the creation of a fixture in my class, something similar to:

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

The problem is that I am using PropertyData , and the second is two input parameters. The fact that I'm trying to automatically create my device as a parameter throws an exception.

Here is the CustomPropertyData data:

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

What are the solutions to this problem?

+5
source share
1 answer

You need to send the data to the PropertyDataAttribute , as shown below:

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

patientId will be 123, patientFirstName will be abc, and SUT will be automatically transmitted using AutoFixture.

CustomPropertyDataAttribute looks good.

+3
source

Source: https://habr.com/ru/post/953999/


All Articles