예제 #1
0
        public void WhenDeleteCalled_ThenRemovesSuperPerson()
        {
            // Arrange
            var superPerson = new SuperPerson()
            {
                Id = 1, Allegiance = "A"
            };

            var mockEntitySet = new Mock <IEntitySet <SuperPerson> >();

            mockEntitySet.Setup(x => x.Remove(superPerson)).Verifiable();

            var mockContext = new Mock <ISuperDatabaseContext>();

            mockContext.Setup(x => x.SuperPeople).Returns(mockEntitySet.Object);

            ISuperDatabaseContext context = mockContext.Object;
            SuperRepository       target  = new SuperRepository(context);

            // Act
            target.Delete(superPerson);

            // Assert
            mockEntitySet.VerifyAll();
        }
예제 #2
0
        public void WhenInsertCalled_ThenAddsSuperPerson()
        {
            // Arrange
            var superPerson = new SuperPerson()
            {
                Id = 1, Allegiance = "A"
            };

            // Note: Moq example marking a call as verifiable, then verifying all at the end

            var mockEntitySet = new Mock <IEntitySet <SuperPerson> >();

            mockEntitySet.Setup(x => x.Add(superPerson)).Verifiable();

            var mockContext = new Mock <ISuperDatabaseContext>();

            mockContext.Setup(x => x.SuperPeople).Returns(mockEntitySet.Object);

            ISuperDatabaseContext context = mockContext.Object;
            SuperRepository       target  = new SuperRepository(context);

            // Act
            target.Insert(superPerson);

            // Assert
            mockEntitySet.VerifyAll();
        }
예제 #3
0
        public void WhenGetCalled_ThenReturnsSuperPerson()
        {
            // Arrange
            var superPerson = new SuperPerson()
            {
                Id   = 1,
                Name = "Name1"
            };

            // Note: Moq example of an mocking a method that takes an argument and returns a value
            // Note: Castle-proxy is under each mock => requires interface or virtual

            var mockContext = new Mock <ISuperDatabaseContext>();

            mockContext.Setup(x => x.SuperPeople.Find(1)).Returns(superPerson);

            ISuperDatabaseContext context = mockContext.Object;
            SuperRepository       target  = new SuperRepository(context);

            // Act
            var actual = target.Get(1);

            // Assert
            Assert.AreSame(superPerson, actual);

            mockContext.Verify(x => x.SuperPeople.Find(1), Times.Once());
        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            SuperPerson superPerson = value as SuperPerson;

            if (superPerson == null)
            {
                return(null);
            }

            return(string.Format("Images/Avatars/{0}.png", superPerson.Name));
        }
예제 #5
0
        public void WhenRankSet_ThenRankUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Rank = 3;

            var actual = target.Rank;

            // Assert
            Assert.AreEqual(3, actual);
        }
예제 #6
0
        public void WhenNameSet_ThenNameUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Name = "Name";

            var actual = target.Name;

            // Assert
            Assert.AreEqual("Name", actual);
        }
예제 #7
0
        public void WhenDamaged_ThenHealthReducedByDamageAmount()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Damage(80);

            var actual = target.Health;

            // Assert
            Assert.AreEqual(920, actual);
        }
예제 #8
0
        public void WhenIntellectSet_ThenIntellectUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Intellect = 321;

            var actual = target.Intellect;

            // Assert
            Assert.AreEqual(321, actual);
        }
예제 #9
0
        public void WhenSpeedSet_ThenSpeedUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Speed = 456;

            var actual = target.Speed;

            // Assert
            Assert.AreEqual(456, actual);
        }
예제 #10
0
        public void WhenResistanceSet_ThenResistanceUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Resistance = 789;

            var actual = target.Resistance;

            // Assert
            Assert.AreEqual(789, actual);
        }
예제 #11
0
        public void WhenHeathSetToZero_ThenIsAliveSetToFalse()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Health = 0;

            bool actual = target.IsAlive;

            // Assert
            Assert.IsFalse(actual);
        }
예제 #12
0
        public void WhenStrengthSet_ThenStrengthUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Strength = 123;

            var actual = target.Strength;

            // Assert
            Assert.AreEqual(123, actual);
        }
예제 #13
0
        public void WhenDamagedGreaterThanHealth_ThenHealthSetToZero()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Damage(80000);

            var actual = target.Health;

            // Assert
            Assert.AreEqual(0, actual);
        }
예제 #14
0
        public void WhenAllegianceSet_ThenAllegianceUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Allegiance = "Allegiance";

            var actual = target.Allegiance;

            // Assert
            Assert.AreEqual("Allegiance", actual);
        }
예제 #15
0
        public void WhenHeathSetToPositive_ThenIsAliveSetToTrue()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            target.Health = 0;

            // Act
            target.Health = 1;

            bool actual = target.IsAlive;

            // Assert
            Assert.IsTrue(actual);
        }
예제 #16
0
        public void WhenRevived_ThenHealthSetToDefault()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            target.Health = 100;

            // Act
            target.Revive();

            var actual = target.Health;

            // Assert
            Assert.AreEqual(1000, actual);
        }
예제 #17
0
        public static void Run()
        {
            Person por;

            por.firstname = "Thiti";
            por.Lastname  = "Mahawannakit";
            por.age       = 22;

            //Console.WriteLine(por);


            SuperPerson prite = new SuperPerson();

            prite.SetData("Tanawatt", "Jituthai", 22);
            prite.GetData();
        }
예제 #18
0
        public void WhenIdSet_ThenIdUpdated()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            // Note: Auto-backed properties will not show code coverage,
            // Note:but they need tests because that is an implementation detail.

            // Act
            target.Id = 4;

            var actual = target.Id;

            // Assert
            Assert.AreEqual(4, actual);
        }
예제 #19
0
        private void WriteSuperPerson(XmlWriter writer, SuperPerson superPerson)
        {
            writer.WriteStartElement("SuperPerson");

            writer.WriteAttributeString("Id", XmlConvert.ToString(superPerson.Id));
            writer.WriteAttributeString("Name", superPerson.Name);
            writer.WriteAttributeString("Allegiance", superPerson.Allegiance);
            writer.WriteAttributeString("Rank", XmlConvert.ToString(superPerson.Rank));
            writer.WriteAttributeString("Health", XmlConvert.ToString(superPerson.Health));
            writer.WriteAttributeString("Strength", XmlConvert.ToString(superPerson.Strength));
            writer.WriteAttributeString("Speed", XmlConvert.ToString(superPerson.Speed));
            writer.WriteAttributeString("Resistance", XmlConvert.ToString(superPerson.Resistance));
            writer.WriteAttributeString("Intellect", XmlConvert.ToString(superPerson.Intellect));

            writer.WriteFullEndElement();
        }
예제 #20
0
        public void WhenHeathSetToDifferentValue_ThenHealthPropertyChangedRaised()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            bool wasPropertyChangedRaised = false;

            target.PropertyChanged += (s, e) =>
            {
                wasPropertyChangedRaised = (e.PropertyName == SuperPerson.PropertyNames.Health);
            };

            // Act
            target.Health = 50;

            // Assert
            Assert.IsTrue(wasPropertyChangedRaised);
        }
예제 #21
0
        public void WhenHeathSetToZero_ThenIsAlivePropertyChangedRaised()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            bool wasPropertyChangedRaised = false;

            target.PropertyChanged += (s, e) =>
            {
                wasPropertyChangedRaised = (e.PropertyName == SuperPerson.PropertyNames.IsAlive);
            };

            // Act
            target.Health = 0;

            // Assert
            Assert.IsTrue(wasPropertyChangedRaised);
        }
예제 #22
0
        public void WhenConstructed_ThenInitialized()
        {
            // Note: Triple-A Pattern

            // Arrange

            // Act
            SuperPerson actual = new SuperPerson();

            // Assert
            Assert.IsNotNull(actual);

            // Note: Example of expected vs. actual

            Assert.AreEqual(1000, actual.Health);
            Assert.AreEqual(0, actual.Id);
            Assert.IsNull(actual.Name);
            Assert.IsTrue(actual.IsAlive);
        }
예제 #23
0
        private SuperPerson ReadSuperPerson(XmlReader reader)
        {
            SuperPerson superPerson = new SuperPerson();

            superPerson.Id         = XmlConvert.ToInt32(reader.GetAttribute("Id"));
            superPerson.Name       = reader.GetAttribute("Name");
            superPerson.Allegiance = reader.GetAttribute("Allegiance");
            superPerson.Rank       = XmlConvert.ToInt32(reader.GetAttribute("Rank"));
            superPerson.Health     = XmlConvert.ToInt32(reader.GetAttribute("Health"));
            superPerson.Strength   = XmlConvert.ToInt32(reader.GetAttribute("Strength"));
            superPerson.Resistance = XmlConvert.ToInt32(reader.GetAttribute("Resistance"));
            superPerson.Intellect  = XmlConvert.ToInt32(reader.GetAttribute("Intellect"));
            superPerson.Speed      = XmlConvert.ToInt32(reader.GetAttribute("Speed"));

            reader.ReadStartElement("SuperPerson");
            reader.ReadEndElement();

            return(superPerson);
        }
예제 #24
0
        public void WhenHeathSetToAnotherPositiveValue_ThenIsAlivePropertyChangedIsNotRaised()
        {
            // Arrange
            SuperPerson target = new SuperPerson();

            target.Health = 50;

            bool wasPropertyChangedRaised = false;

            target.PropertyChanged += (s, e) =>
            {
                wasPropertyChangedRaised = (e.PropertyName == SuperPerson.PropertyNames.IsAlive);
            };

            // Act
            target.Health = 70;

            // Assert
            Assert.IsFalse(wasPropertyChangedRaised);
        }
예제 #25
0
        public void WhenHeathSet_ThenHealthUpdated()
        {
            // Note: Example of target vs. actual

            // Arrange
            SuperPerson target = new SuperPerson();

            // Act
            target.Health = 50;

            // Note: var is useful to keep tests clean,
            // Note: but var can make type validation easy to miss
            // Note: show "Error" in AreEqual as example.

            var actual = target.Health;

            // Note: Don't want too many asserts per test.

            // Assert
            Assert.AreEqual(50, actual);
        }
예제 #26
0
        public void WhenUpdateCalled_ThenUpdatesSuperPerson()
        {
            // Arrange
            var superPerson = new SuperPerson()
            {
                Id = 1, Allegiance = "A"
            };

            var mockContext = new Mock <ISuperDatabaseContext>();

            mockContext.Setup(x => x.SetEntityStateModified(superPerson)).Verifiable();

            ISuperDatabaseContext context = mockContext.Object;
            SuperRepository       target  = new SuperRepository(context);

            // Act
            target.Update(superPerson);

            // Assert
            mockContext.VerifyAll();
        }
예제 #27
0
        public void WhenHeathSetToSameValue_ThenHealthPropertyChangedIsNotRaised()
        {
            // Note: Example of testing a non-visible code-path

            // Arrange
            SuperPerson target = new SuperPerson();

            target.Health = 50;

            bool wasPropertyChangedRaised = false;

            target.PropertyChanged += (s, e) =>
            {
                wasPropertyChangedRaised = (e.PropertyName == SuperPerson.PropertyNames.Health);
            };

            // Act
            target.Health = 50;

            // Assert
            Assert.IsFalse(wasPropertyChangedRaised);
        }
예제 #28
0
 /// <summary>
 /// Sets the super person's entity state to modified.
 /// </summary>
 /// <param name="superPerson">The super person set as modified.</param>
 void ISuperDatabaseContext.SetEntityStateModified(SuperPerson superPerson)
 {
     this.Entry(superPerson).State = System.Data.EntityState.Modified;
 }