Skip to content

Automatically fill a type that inherits from List<T> #89

@vavranic

Description

@vavranic

I'm using your handy ObjectFiller for unit testing legacy code. This legacy code includes a lot of classes that inherit from List. Some of these list types include additional properties to fill. Please consider adding more support for such types.

public class EntityA
{
    public string Name { get; set; }
    public int ID { get; set; }
    public EntityBList Bs { get; set; }
}
public class EntityB
{
    public string Name { get; set; }
    public Guid ID { get; set; }
}
public class EntityBList : List<EntityB>
{
    public DateTime SomeDate { get; set; }
}

My ultimate goal is to call new Filler<EntityA>().Create() and get back an EntityA object whose Bs property has both SomeDate filled and also has some EntityB items whose properties are filled. As is, I end up manually coding the EntityBList handling using something like this:
Setup().OnProperty(x => x.Bs).Use(delegate () { EntityBList ebl = new EntityBList(); ebl.SomeDate = DateTime.Now; Filler<EntityB> fb = new Filler<EntityB>(); ebl.AddRange(fb.Create(4)); return ebl; }); Since we have a great many of these types, that ends up being a lot of code.

I think this would require several enhancements.

First, Filler doesn't seem to recognize EntityBList as an IList to be filled. Filler sees that this inherits IList. Filler then checks whether the type has generic arguments, but this one does not. It's base class has the generic arguments.

Second, given that it doesn't treat this type as a list, Filler attempts to set the Capacity and Item properties. Those throw exceptions. I can set it up to ignore the Capacity property by using Setup().SetupFor<EntityBList>().OnProperty(x => x.Capacity).IgnoreIt() but have not figured out a way to ignore the special Item property which represents the this[int] indexer.

Third, I suspect that Filler either treats the type as a list to be filled or treats it as an object whose properties should be filled, and not both. I'd want it to fill the list and also fill the properties.

We do also have a couple types that inherit from Dictionary<>. It'd be nice to cover those in the same way.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions