elena1234

Mocking sintax with Moq Library (UnitTesting February from OOP)

May 16th, 2021 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1.   namespace Tests
  2. {
  3.     [TestFixture]
  4.     public class HeroTests
  5.     {
  6.         const int experiance = 200;
  7.  
  8.         [Test]
  9.         public void AttackLogicShouldWorkCorrectly()
  10.         {
  11.             // Arrange
  12.             var weaponMock = Mock.Of<IWeapon>();
  13.  
  14.             var targetMock = new Mock<ITarget>();
  15.             targetMock.Setup(t => t.IsDead())
  16.                 .Returns(true);
  17.             targetMock.Setup(t => t.GiveExperience())
  18.                 .Returns(200);
  19.  
  20.             var hero = new Hero("TestHero", weaponMock);
  21.  
  22.             // Act
  23.             hero.Attack(targetMock.Object);
  24.  
  25.             // Assert
  26.             Assert.That(hero.Experience, Is.EqualTo(experiance));
  27.         }
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. //
  34.  var fakeTarget = new Mock<ITarget>();
  35.             fakeTarget.SetUp(x => x.TakeAttack(It.IsAny<int>()))
  36.                 .Callback(() => hero.Weapon.DirabilityPoints -= 1);
  37.             fakeTarget.SetUp(x => x.Health)
  38.             .Returns(0);
Add Comment
Please, Sign In to add comment