How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

后端 未结 3 1691
孤城傲影
孤城傲影 2021-01-05 08:47

I am using the [AutoNSubstituteData] attribute, which was posted here:

AutoFixture, xUnit.net, and Auto Mocking

I would like to combine this wit

3条回答
  •  渐次进展
    2021-01-05 09:07

    There are two ways to do this:

    Option 1 - Using AutoFixture.Xunit and the CompositeDataAttribute class:

    internal class AutoNSubstituteDataAttribute : AutoDataAttribute
    {
        internal AutoNSubstituteDataAttribute()
            : base(new Fixture().Customize(new AutoNSubstituteCustomization()))
        {
        }
    }
    
    internal class AutoNSubstitutePropertyDataAttribute : CompositeDataAttribute
    {
        internal AutoNSubstitutePropertyDataAttribute(string propertyName)
            : base(
                new DataAttribute[] { 
                    new PropertyDataAttribute(propertyName), 
                    new AutoNSubstituteDataAttribute() })
        {
        }
    }
    

    Define the test cases as below:

    public class Scenario
    {
        public static IEnumerable InvalidInvariantCase1
        {
            get
            {
                yield return new string[] { null };
            }
        }
    
        public static IEnumerable InvalidInvariantCase2
        {
            get
            {
                yield return new string[] { string.Empty };
            }
        }
    
        public static IEnumerable InvalidInvariantCase3
        {
            get
            {
                yield return new string[] { " " };
            }
        }
    }
    

    Then declare the parameterized test as:

    public class Scenarios
    {
        [Theory]
        [AutoNSubstitutePropertyData("InvalidInvariantCase1")]
        [AutoNSubstitutePropertyData("InvalidInvariantCase2")]
        [AutoNSubstitutePropertyData("InvalidInvariantCase3")]
        public void AParameterizedTest(
            string invalidConnString,
            IDeal deal,
            IDbConnection conn,
            IDb db,
            ISender sender,
            string query)
        {
        }
    }
    

    Please note that the parameterized parameter invalidConnString have to be declared before the other parameters.

    Option 2 - Using Exude:

    public class Scenario
    {
        public void AParameterizedTest(
            IDeal deal,
            IDbConnection conn,
            IDb db,
            ISender sender,
            string invalidConnString,
            string query)
        {
        }
    
        [FirstClassTests]
        public static TestCase[] RunAParameterizedTest()
        {
            var testCases = new [] 
            {
                new 
                {
                    invalidConnString = (string)null
                },
                new
                {
                    invalidConnString = string.Empty
                },
                new
                {
                    invalidConnString = " "
                }
            };
    
            var fixture = new Fixture()
                .Customize(new AutoNSubstituteCustomization());
    
            return testCases
                .Select(tc =>
                    new TestCase(
                        s => s.AParameterizedTest(
                            fixture.Create(),
                            fixture.Create(),
                            fixture.Create(),
                            fixture.Create(),
                            tc.invalidConnString,
                            fixture.Create())))
                .ToArray();
        }
    }
    

提交回复
热议问题