mardi 23 mai 2017

Autofac , unit-testing ,c# - Difference between Autofac Moq and FakeItEasy integration package

Can anyone help me differentiate between Autofac Moq and FakeItEasy integration packages. My demo project uses Autofac for its dependencies. I want to change my unit test cases also to use Autofac Moq/FakeItEasy integration project. I am currently using Autofac MOQ

I was referring link : http://ift.tt/1G9vw3d helpful but I couldn't run my test successfully.

In the example below IDAL and IDATE are my dependencies and IAGE is a parameter to my unit test function CalculateMatruity(age).

Questions: 1 Unit test is not taking my fake parameters IDAl, IDATE. 2. Also, can any please advise how to pass parameters to the unit test function. 3. Am I passing the parameter correctly my instantiating New person() object?

my sample unit test is:

[TestMethod]
        public void TestGenericHelper()
        {
            //arrange
            using (var mock = AutoMock.GetLoose())
            {

                mock.Mock<IDAL>().Setup(x => x.GetMaturityConfiguration()).Returns("0=Child|13=Teen|18=Adult");
                mock.Mock<IAge>().Setup(x => x.Birthdate).Returns(DateTime.Parse("1987-06-16"));
                mock.Mock<IDate>().Setup(x => x.Date).Returns(Convert.ToDateTime("2000-01-01"));

                var sut = mock.Create<GenericHelper>(); 

                String expected = "Teen";

                Person age = new Person();
                   age.Birthdate = DateTime.Parse("1987-06-16"); 

                //act
                 //  mock.Provide<IGenericHelper, GenericHelper>(new NamedParameter("AgeObj", DateTime.Parse("1987-06-16")));

                String actual = sut.CalculateMatruity(age); 

                //assert
                mock.Mock<IDAL>().Verify(x => x.GetMaturityConfiguration());
                mock.Mock<IAge>().Verify(x => x.Birthdate);
                mock.Mock<IDate>().Verify(x => x.Date);
                Assert.AreEqual(expected, actual);
            }
        }

Previously my test was:

[TestMethod]
        public void TestGenericHelper()
        {
            Person age = new Person();
            age.Birthdate = DateTime.Parse("1987-06-16");     
            String expected = "Teen"; 

            DateTime FakeDate = Convert.ToDateTime("2000-01-01");
            String fake = "0=Child|13=Teen|18=Adult";

            FakeDate DateHelperObj = new FakeDate(FakeDate);
            FakeAgeDAL fakeObj = new FakeAgeDAL(fake);

            GenericHelper GenericHelperObj = new GenericHelper(fakeObj, DateHelperObj);
            String actual = GenericHelperObj.CalculateMatruity(age);

            Assert.AreEqual(expected, actual);
        }

Aucun commentaire:

Enregistrer un commentaire