AutoFixture: how to create polymorphic objects with ISpecimenBuilder

Jim

I am a little at a loss as to how to code this better. The use of reflection to obtain the Create<T> on the specimen context is terrible. Sadly CreateAnonymous is deprecated... So I can't think of a better way.

IX is an interface, and the specimen builder is creating random instances of concrete classes implementing IX for testing purposes.

/// <summary>
/// A specimen builder that creates random X messages.
/// </summary>
public class XMessageBuilder : ISpecimenBuilder
{
    // for brevity assume this has types implementing IX
    private readonly Type[] types;
    private readonly Random random = new Random();

    // THERE MUST BE A BETTER WAY TO DO THIS?
    // CreateAnonymous is deprecated :-(
    public IX CreateSampleMessage(ISpecimenContext context)
    {
        var rm = this.types.ElementAt(this.random.Next(0, this.types.Length));
        var method = typeof(SpecimenFactory).GetMethod("Create", new[] { typeof(ISpecimenContext) });
        var generic = method.MakeGenericMethod(rm);
        var instance = generic.Invoke(null, new object[] { context });
        return (IX)instance;
    }

    public object Create(object request, ISpecimenContext context)
    {
        var parameter = request as ParameterInfo;
        if (parameter == null)
            return new NoSpecimen();

        if (parameter.ParameterType == typeof(IX))
            return this.CreateSampleMessage(context);

        if (parameter.ParameterType == typeof(IX[]))
        {
            var array = new IX[10];
            for (int index = 0; index < array.Length; index++)
                array[index] = this.CreateSampleMessage(context);
            return array;
        }

        return new NoSpecimen();
    }
Mark Seemann

Something like this ought to work:

public IX CreateSampleMessage(ISpecimenContext context)
{
    var rm = this.types.ElementAt(this.random.Next(0, this.types.Length));
    return (IX)context.Resolve(rm);
}

(I haven't tried to compile and run a repro, though, so I may have made an error somewhere.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to create a Generic Model without Id for AutoFixture?

From Dev

Let AutoFixture create DateTime in UTC?

From Dev

How to tell Autofixture to create objects with different ID?

From Dev

AutoFixture create property with internal setter

From Dev

How can I create a controller for polymorphic association using ruby on rails?

From Dev

AutoFixture and the Create method

From Dev

How to fix a range on some properties when create a TestClass by AutoFixture

From Dev

How to see if two polymorphic objects are equal?

From Dev

how to create an object using another as a source in Autofixture?

From Dev

Create testdata with List<string>, Autofixture

From Dev

How to generate a dictionary with Autofixture

From Dev

How to create a record type more than one polymorphic variables

From Dev

When using a RegEx in AutoFixture ISpecimenBuilder, Why do I always get back the same value?

From Dev

Create an event stream of polymorphic functions - possible? If yes, how?

From Dev

How to create a polymorphic unboxed array within ST monad?

From Dev

Create an EF entity stub with AutoFixture

From Dev

How to create a 'has_many through' association if a polymorphic association is involved?

From Dev

How to create seeds of a polymorphic relationship

From Dev

Can Autofixture create an anonymous type?

From Dev

How to create a SortedList<Tkey, TValue> with AutoFixture

From Dev

How to create a polymorphic type for UnixStream and TcpStream

From Dev

AutoFixture create property with internal setter

From Dev

AutoFixture and the Create method

From Dev

how to create an object using another as a source in Autofixture?

From Dev

Create testdata with List<string>, Autofixture

From Dev

How can I create an array with polymorphic data?

From Dev

How to create a polymorphic numeric value from a String

From Dev

Why does AutoFixture's AutoMoqData not create mock objects?

From Dev

How to serialize list of polymorphic objects using built_value and StandardJsonPlugin?

Related Related

  1. 1

    How to create a Generic Model without Id for AutoFixture?

  2. 2

    Let AutoFixture create DateTime in UTC?

  3. 3

    How to tell Autofixture to create objects with different ID?

  4. 4

    AutoFixture create property with internal setter

  5. 5

    How can I create a controller for polymorphic association using ruby on rails?

  6. 6

    AutoFixture and the Create method

  7. 7

    How to fix a range on some properties when create a TestClass by AutoFixture

  8. 8

    How to see if two polymorphic objects are equal?

  9. 9

    how to create an object using another as a source in Autofixture?

  10. 10

    Create testdata with List<string>, Autofixture

  11. 11

    How to generate a dictionary with Autofixture

  12. 12

    How to create a record type more than one polymorphic variables

  13. 13

    When using a RegEx in AutoFixture ISpecimenBuilder, Why do I always get back the same value?

  14. 14

    Create an event stream of polymorphic functions - possible? If yes, how?

  15. 15

    How to create a polymorphic unboxed array within ST monad?

  16. 16

    Create an EF entity stub with AutoFixture

  17. 17

    How to create a 'has_many through' association if a polymorphic association is involved?

  18. 18

    How to create seeds of a polymorphic relationship

  19. 19

    Can Autofixture create an anonymous type?

  20. 20

    How to create a SortedList<Tkey, TValue> with AutoFixture

  21. 21

    How to create a polymorphic type for UnixStream and TcpStream

  22. 22

    AutoFixture create property with internal setter

  23. 23

    AutoFixture and the Create method

  24. 24

    how to create an object using another as a source in Autofixture?

  25. 25

    Create testdata with List<string>, Autofixture

  26. 26

    How can I create an array with polymorphic data?

  27. 27

    How to create a polymorphic numeric value from a String

  28. 28

    Why does AutoFixture's AutoMoqData not create mock objects?

  29. 29

    How to serialize list of polymorphic objects using built_value and StandardJsonPlugin?

HotTag

Archive