1)
Setup formatted data in time of object creation.
Let’s assume one scenario where we need to setup formatted
data in time of using Auto Fixture. For example, we have Age property of
integer type in class and we want to specify some range value to be generated
in time of object creation using Auto Fixture.
The good news is, Auto Fixture supports attribute to
generate automated value. We can decorate our POCO class like following.
public class Person
{
[Range(18,100)]
public int Age { get; set; }
[StringLength(10)]
public string Name { get; set; }
public DateTime RegDate { get; set; }
}
Here is the code to create object using Auto Fixture.
[TestMethod]
public void Test_Function()
{
var fixture = new Fixture();
var person = fixture.Create<Person>();
}
And we are seeing that the value has created based on
attribute specified in POCO class.
2) Ignore any property from being populated
This is another important setup you might need in time of
object creation. Let’s assume one scenario where you want not to populate any
property in time of object creation or you want to set some default value to
some property.
Let’s have a look in below example. We are using same POCO
class
public class Person
{
[Range(18,100)]
public int Age { get; set; }
[StringLength(10)]
public string Name { get; set; }
public DateTime RegDate { get; set; }
}
Now, we don’t want to set “Age” property in time of object
creation and the RegDate should to today’s date. To implement same we can setup
fixture like this.
[TestMethod]
public void Test_Function()
{
var fixture = new Fixture();
var person = fixture.Build<Person>().
Without(f=>f.Age).
Do(f=>f.RegDate = DateTime.Now).CreateAnonymous();
}
No comments:
Post a Comment