Ejemplo n.º 1
0
        public void CalculateAge_Born19820520_Today_Age29()
        {
            //Arrange datetime now, today is 2 feb 2012
            var dt = new DateTime(2012, 2, 5);

            Mock.Arrange(() => DateTime.Now).Returns(dt);

            //Act
            var result = Pioneers.CalculateAge(new DateTime(1982, 05, 20));

            //Asset
            Assert.IsTrue(result == 29);
        }
Ejemplo n.º 2
0
        public void CalculateAge_Born19820520_Today_Age29_NonMocked()
        {
            //Arrange datetime now, today is 2 feb 2012
            var dt = new DateTime(2012, 2, 5);

            /* * * * * * * * * * * * Test will fail after some time.. * * * * * * * * * * * * * * * */

            //Act
            var result = Pioneers.CalculateAge(new DateTime(1982, 05, 20));

            //Assert
            Assert.IsFalse(result == 29); //Currently is 30, so it's not 29 any more
        }
Ejemplo n.º 3
0
        public void GetDutchPioneers_ContextWithSeveralPioneersAndNationalities_DutchPioneersOnly_NonMocked()
        {
            // Arrange. Mock without a mocking framework
            Pioneers pioneers = new Pioneers(Source.XmlFile); //1st problem, you create a real object, is this xmlfile available???

            //Arrange. private _datacontext object
            PropertyInfo prop = typeof(Pioneers).GetProperty("_dataContext", BindingFlags.NonPublic | BindingFlags.Instance);

            prop.SetValue(pioneers, DataSource.Pioneers(), null);

            //Act.
            var result = pioneers.GetDutchPioneers();

            //Assert
            Assert.IsTrue(result.All(p => p.Nationality.Equals("NL")));
        }
Ejemplo n.º 4
0
        public void GetDutchPioneers_ContextWithSeveralPioneersAndNationalities_DutchPioneersOnly()
        {
            // Arrange. The CallOriginal method marks a mocked method/property call that
            // should execute the original method/property implementation
            Pioneers pioneers = Mock.Create <Pioneers>(Behavior.CallOriginal);

            //Arrange all function you like to execute a different implementation.
            Mock.NonPublic.Arrange <IList <AviationPioneer> >(pioneers, "_dataContext")
            .Returns(DataSource.Pioneers());

            // Act. Call the orginal implementation, you already arranged a different
            // implementation for the _datacontext..
            var result = pioneers.GetDutchPioneers();

            //Assert
            Assert.IsTrue(result.All(p => p.Nationality.Equals("NL")));
        }
Ejemplo n.º 5
0
        public void OlderThan65_ContextWithSeveralPioneersDeadAndAlive_PioneersCurrentlyAreOlderThan65()
        {
            //Arrange. The CallOriginal method marks a mocked method/property call that
            //should execute the original method/property implementation
            Pioneers pioneers = Mock.Create <Pioneers>(Behavior.CallOriginal);

            //Arrange. private _datacontext object
            Mock.NonPublic.Arrange <IList <AviationPioneer> >(pioneers, "_dataContext")
            .Returns(DataSource.Pioneers());

            //Mock the CalculateAge function used by the OlderThan65 function.
            //The CalculateAge function is already tested in CalculateAge_* test methods

            /* You can arrange the CalculateAge function for each pioneers available in the context
             * like this:
             * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1931, 5, 13)))
             *  .Returns(81); //date of birth
             *
             * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1982, 5, 20)))
             *  //when set this return value to 66 the result count is 3..
             *  .Returns(30);
             *
             * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1930, 8, 5)))
             *  .Returns(82);
             */

            //Or make your own test implementation of the calculateAge function.
            Mock.Arrange(() => Pioneers.CalculateAge(Arg.IsAny <DateTime>()))
            .Returns((DateTime birth) =>
            {
                //Your test implementation here, for example
                return(2012 - birth.Year);
            });

            //Act. Call the orginal implementation, you already arranged a different
            //implementation for the _datacontext.. and the CalculateAge function...
            var result = pioneers.OlderThan65();

            //Assert
            //Based on the test value for calculateAge and the test context
            //there are two person older than 65
            Assert.IsTrue(result.Count() == 2);
        }
Ejemplo n.º 6
0
        public void OlderThan65_ContextWithSeveralPioneersDeadAndAlive_PioneersCurrentlyAreOlderThan65_NonMocked()
        {
            // Arrange. Mock without a mocking framework
            Pioneers pioneers = new Pioneers(Source.XmlFile); //1st problem, you create a real object, is this xmlfile available???

            //Arrange. private _datacontext object
            PropertyInfo prop = typeof(Pioneers).GetProperty("_dataContext", BindingFlags.NonPublic | BindingFlags.Instance);

            prop.SetValue(pioneers, DataSource.Pioneers(), null);

            /* We can't mock the calculate function!
             * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1931, 5, 13)))
             *  .Returns(....*/

            /* * * * * * * * * * * * Test will fail after some time.. * * * * * * * * * * * * * * * */

            //Act.
            var result = pioneers.OlderThan65();

            //Assert
            Assert.IsTrue(result.Count() == 2);
        }