Showing posts with label Fixture. Show all posts
Showing posts with label Fixture. Show all posts

Saturday, September 19, 2015

2 important setups of Auto Fixture to generate Fake Object.



      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();
        }

Friday, September 11, 2015

Fixture to Generate automated data in unit testing.



Automated or test data is very essential to unit test the functionality. We can use Fixture to generate automated random data in time of unit testing.
You can add AutoFixture framework from nugget package manager . 



Let’s see one simple example to generate automated data using Fixture. Here is my small add functionality which will return addition of two numbers.


class TestClass
    {
        public int Add( int a, int b)
        {
            return a + b;
        }
    }

And here is code to test the functionality.

[TestClass]
    public class UnitTestClass
    {
        [TestMethod]
        public void Add_Test_Function()
        {
            var fixture = new Fixture();
            int x = fixture.CreateAnonymous<int>();
            int y = fixture.CreateAnonymous<int>();
            var testObject = new Code.TestClass();
            Assert.AreEqual(testObject.Add(x,y),x+y);
        }
    }

In Add_Test_Function , we have created object of Fixture class and then calling CreateAnonymous<T> function to generate automated int data because T is specified as integer.
Ok, so we have generated primitive data using fixture, but how we can generate user defined object ? 

In this example we will generate random user defied object.
Here is our class for which we would like to generate objects.

class TestClass
    {
        public int Id { get; set; }
        public string  Name { get; set; }

        public void AddData(IEnumerable<TestClass> param)
        {
           
        }
    }

Here is test functionality to call AddData function by generating random object of TestClass class.

[TestClass]
    public class UnitTestClass
    {
        [TestMethod]
        public void Add_Test_Function()
        {
            var fixture = new Fixture();
            fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

            new Code.TestClass().AddData(fixture.CreateMany<Code.TestClass>(new Random().Next(1,20)));
        }
    }

And we are seeing that it has generated few random objects.