Linking Rules


These rules can be applied between any two properties of the same type.

class SampleData
{
    public string FirstValue { get; set; }
    public string SecondValue { get; set; }
}

Link

Set a property to have the same value as another property.

var sampleData = Fabricator.Setup<SampleData>()
                           .Link(x => x.SecondValue).To(x => x.FirstValue)
                           .Generate();

// sampleData.SecondValue= sampleData.FirstValue

Notes
  • A property cannot be linked to itself
  • A set of properties cannot be involved in a direct or indirect circular dependency

Map


These rules can be applied between any two properties of any type.

Map Using Lookup


Set a property to have a specific value according to a lookup from another property.

var mapping = new Dictionary<string, string> { { "FirstValue1", "A" }, { "FirstValue2", "B" } };
var sampleData = Fabricator.Setup<SampleData>()
                           .For(x => x.SecondValue).Map(x => x.FirstValue).Using(mapping)
                           .Generate();

// sampleData.FirstValue= "FirstValue1"
// sampleData.SecondValue= "A"

Map Using Method


Set a property to have a specific value according to a lookup from another property.

var mapping = new Dictionary<string, string> { { "FirstValue1", "A" }, { "FirstValue1", "B" }, { "FirstValue1", "C" } };
var sampleData = Fabricator.Setup<SampleData>()
                           .For(x => x.SecondValue).Map(x => x.FirstValue).Using(x => "MappedTo" + x)
                           .Generate();

// sampleData.FirstValue= "FirstValue1"
// sampleData.SecondValue= "MappedToFirstValue1"

Notes
  • A property cannot be mapped to itself
  • A set of properties cannot be involved in a direct or indirect circular dependency

Last edited Nov 13, 2010 at 4:29 AM by JRoughan, version 7

Comments

No comments yet.