Advertisement
Guest User

Unit Testing - 16 August OOP Exam

a guest
Aug 26th, 2020
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. using System;
  2. using NUnit.Framework;
  3.  
  4. namespace Computers.Tests
  5. {
  6. public class Tests
  7. {
  8. private Computer defaultComputer;
  9. private readonly string defaultManufacturer = "Asus";
  10. private readonly string defaultModel = "X55";
  11. private readonly decimal defaultPrice = 1500;
  12.  
  13. private ComputerManager computerManager;
  14. [SetUp]
  15. public void Setup()
  16. {
  17. this.defaultComputer = new Computer(defaultManufacturer, defaultModel, defaultPrice);
  18. this.computerManager = new ComputerManager();
  19. }
  20.  
  21. //Computer Tests
  22. [Test]
  23. public void CheckIfComputerConstructorWorksCorrectly()
  24. {
  25. Assert.AreEqual( this.defaultPrice, this.defaultComputer.Price);
  26. Assert.AreEqual( this.defaultManufacturer, this.defaultComputer.Manufacturer);
  27. Assert.AreEqual( this.defaultModel, this.defaultComputer.Model);
  28. }
  29.  
  30. //ComputerManager Tests
  31.  
  32. [Test]
  33. public void CheckIfConstructorWorksCorrectly()
  34. {
  35. Assert.That(this.computerManager.Count, Is.EqualTo(0));
  36. Assert.That(this.computerManager.Computers, Is.Empty);
  37. }
  38. [Test]
  39. public void CheckIfCountWorksCorrectly()
  40. {
  41. this.computerManager.AddComputer(this.defaultComputer);
  42. Assert.That(this.computerManager.Count, Is.EqualTo(1));
  43. Assert.That(this.computerManager.Computers, Has.Member(this.defaultComputer));
  44. }
  45.  
  46. [Test]
  47. public void AddShouldThrowExceptionWithExistingComputer()
  48. {
  49. this.computerManager.AddComputer(this.defaultComputer);
  50. Assert.Throws<ArgumentException>(() =>
  51. {
  52. this.computerManager.AddComputer(this.defaultComputer);
  53. }, "This computer already exists.");
  54. }
  55.  
  56. [Test]
  57. public void AddShouldIncreaseCountWhenSuccessful()
  58. {
  59. this.computerManager.AddComputer(this.defaultComputer);
  60.  
  61. Assert.That(this.computerManager.Count, Is.EqualTo(1));
  62. Assert.That(this.computerManager.Computers, Has.Member(this.defaultComputer));
  63. }
  64.  
  65. [Test]
  66. public void AddShouldThrowExceptionWithNullValue()
  67. {
  68. Assert.Throws<ArgumentNullException>(() =>
  69. {
  70. this.computerManager.AddComputer(null);
  71. }, "Can not be null.");
  72. }
  73.  
  74. [Test]
  75. public void RemoveComputerShouldRemoveCorrectComputer()
  76. {
  77. this.computerManager.AddComputer(this.defaultComputer);
  78. var result = this.computerManager.RemoveComputer(this.defaultManufacturer, this.defaultModel);
  79.  
  80. Assert.AreSame(this.defaultComputer, result);
  81. Assert.That(this.computerManager.Count, Is.EqualTo(0));
  82. }
  83.  
  84. [Test]
  85. public void RemoveShouldThrowExceptionWithInvalidComputer()
  86. {
  87. Assert.Throws<ArgumentException>(() =>
  88. {
  89. this.computerManager.RemoveComputer(this.defaultManufacturer, "Acer");
  90. }, "There is no computer with this manufacturer and model.");
  91. }
  92.  
  93. [Test]
  94. public void GetComputerShouldWorkCorrectly()
  95. {
  96. this.computerManager.AddComputer(this.defaultComputer);
  97. var result = this.computerManager.GetComputer(this.defaultManufacturer, this.defaultModel);
  98.  
  99. Assert.AreSame(this.defaultComputer, result);
  100. }
  101.  
  102. [Test]
  103. public void GetComputerShouldThrowExceptionWithInvalidData()
  104. {
  105. this.computerManager.AddComputer(this.defaultComputer);
  106. Assert.Throws<ArgumentException>(() =>
  107. {
  108. this.computerManager.GetComputer(
  109. "HP", "XC300");
  110. }, "There is no computer with this manufacturer and model.");
  111. }
  112.  
  113. [Test]
  114. public void GetComputerShouldThrowExceptionWithNullManufacturer()
  115. {
  116. Assert.Throws<ArgumentNullException>(() => { this.computerManager.GetComputer(null, this.defaultModel); });
  117. }
  118.  
  119. [Test]
  120. public void GetComputerShouldThrowExceptionWithNullModel()
  121. {
  122. Assert.Throws<ArgumentNullException>(() =>
  123. {
  124. this.computerManager.GetComputer(this.defaultManufacturer, null);
  125. });
  126. }
  127.  
  128. [Test]
  129. public void GetByManufacturerShouldThrowExceptionWithNullManufacturer()
  130. {
  131. Assert.Throws<ArgumentNullException>(() =>
  132. {
  133. this.computerManager.GetComputersByManufacturer(null);
  134. });
  135. }
  136.  
  137. [Test]
  138. public void GetAllByManufacturerShouldReturnCorrectCollection()
  139. {
  140. this.computerManager.AddComputer(this.defaultComputer);
  141. this.computerManager.AddComputer(new Computer(this.defaultManufacturer, "K210", 899.99m));
  142. this.computerManager.AddComputer(new Computer(this.defaultManufacturer, "P34", 420));
  143. var collection = this.computerManager.GetComputersByManufacturer(this.defaultManufacturer);
  144.  
  145. Assert.That(collection.Count, Is.EqualTo(3));
  146.  
  147. }
  148.  
  149. [Test]
  150. public void GetAllByManufacturerShouldReturnZeroWithNoMatches()
  151. {
  152. var collection = this.computerManager.GetComputersByManufacturer("Mac");
  153.  
  154. Assert.That(collection.Count, Is.EqualTo(0));
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement