GenFu icon indicating copy to clipboard operation
GenFu copied to clipboard

Order of class properties definition affects filler result.

Open mdmoura opened this issue 9 years ago • 2 comments

Hello,

The order of the properties in a class affects the filler result:

public class Post {    
  public String Slug { get; set; }
  public String Title { get; set; }
}

[Fact]
public void TitleAndSlugShouldBeEqual() {
  Int32 index = 0;
  A.Configure<Post>() 
   .Fill(x => x.Title, x =>
      new List<String> {
        "A", "B"
      }[index++])
   .Fill(x => x.Slug, x => x.Title);
  List<Post> posts = A.ListOf<Post>(2);
  Assert.Equal(2, posts.Count(x => x.Slug == x.Title));
}

This returns false since the Titles are "A" and "B" but the slugs are null.

If I swap the order of Slug and Title in the class definition than it works fine:

public class Post {    
  public String Title { get; set; }
  public String Slug { get; set; }
}

Any idea why this happens? It took me ages to find this bug ... It is far from obvious.

Thank You, Miguel

mdmoura avatar Mar 02 '16 21:03 mdmoura

@mdmoura Ran into this as well.

Though I'm still getting acquainted with the source code, it appears the setting of properties is done in the order returned by GetAllProperties() (see code) which walks the object graph and simply gets all TypeInfo.DeclaredProperties.

So if you wish for GenFu to set the same random value to two different properties make sure your Fill() configuration is for the "latter" property defined in your type/object graph.

miguellira avatar Apr 08 '16 20:04 miguellira

Yes, thanks for commenting @miguellira, you've captured that well.

And yes, @mdmoura, what you're seeing has caused a bit of confusion. It's something that is not intentional, so we'll mark this as a bug and try to get to it at some point.

Two workarounds:

  1. Swap the order of your properties on your class definition, as you've done (total PITA)
  2. Swap the order of your fillers/expressions when configuring GenFu.

Hope this helps some.

Cheers.

MisterJames avatar Apr 09 '16 16:04 MisterJames