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(); }
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(); }
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)); }
public void WhenRankSet_ThenRankUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Rank = 3; var actual = target.Rank; // Assert Assert.AreEqual(3, actual); }
public void WhenNameSet_ThenNameUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Name = "Name"; var actual = target.Name; // Assert Assert.AreEqual("Name", actual); }
public void WhenDamaged_ThenHealthReducedByDamageAmount() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Damage(80); var actual = target.Health; // Assert Assert.AreEqual(920, actual); }
public void WhenIntellectSet_ThenIntellectUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Intellect = 321; var actual = target.Intellect; // Assert Assert.AreEqual(321, actual); }
public void WhenSpeedSet_ThenSpeedUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Speed = 456; var actual = target.Speed; // Assert Assert.AreEqual(456, actual); }
public void WhenResistanceSet_ThenResistanceUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Resistance = 789; var actual = target.Resistance; // Assert Assert.AreEqual(789, actual); }
public void WhenHeathSetToZero_ThenIsAliveSetToFalse() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Health = 0; bool actual = target.IsAlive; // Assert Assert.IsFalse(actual); }
public void WhenStrengthSet_ThenStrengthUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Strength = 123; var actual = target.Strength; // Assert Assert.AreEqual(123, actual); }
public void WhenDamagedGreaterThanHealth_ThenHealthSetToZero() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Damage(80000); var actual = target.Health; // Assert Assert.AreEqual(0, actual); }
public void WhenAllegianceSet_ThenAllegianceUpdated() { // Arrange SuperPerson target = new SuperPerson(); // Act target.Allegiance = "Allegiance"; var actual = target.Allegiance; // Assert Assert.AreEqual("Allegiance", actual); }
public void WhenHeathSetToPositive_ThenIsAliveSetToTrue() { // Arrange SuperPerson target = new SuperPerson(); target.Health = 0; // Act target.Health = 1; bool actual = target.IsAlive; // Assert Assert.IsTrue(actual); }
public void WhenRevived_ThenHealthSetToDefault() { // Arrange SuperPerson target = new SuperPerson(); target.Health = 100; // Act target.Revive(); var actual = target.Health; // Assert Assert.AreEqual(1000, actual); }
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(); }
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); }
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(); }
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); }
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); }
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); }
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); }
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); }
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); }
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(); }
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); }
/// <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; }