PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Source/FizzWare.NBuilder.FunctionalTests/SingleObjectBuilderTests.cs

https://github.com/garethdown44/nbuilder
C# | 257 lines | 204 code | 41 blank | 12 comment | 0 complexity | d669411b005f324400c658e4f2cd25bb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FizzWare.NBuilder.FunctionalTests.Model;
  6. using FizzWare.NBuilder.FunctionalTests.Support;
  7. using NUnit.Framework;
  8. namespace FizzWare.NBuilder.FunctionalTests
  9. {
  10. // Note: The assertions are intentionally verbose to show how NBuilder works
  11. [TestFixture]
  12. public class SingleObjectBuilderTests
  13. {
  14. private interface IMyInterface { }
  15. private abstract class MyAbstractClass { }
  16. [SetUp]
  17. public void SetUp()
  18. {
  19. // Need to call this explicitly here to overcome a bug in resharper's test runner
  20. new SetupFixture().SetUp();
  21. Database.Clear();
  22. }
  23. [Test]
  24. public void CreatingAnObject()
  25. {
  26. var builderSetup = new BuilderSetup();
  27. var product = new Builder<Product>(builderSetup).CreateNew().Build();
  28. Assert.That(product.Id, Is.EqualTo(1));
  29. Assert.That(product.Title, Is.EqualTo("Title1"));
  30. Assert.That(product.Description, Is.EqualTo("Description1"));
  31. Assert.That(product.PriceBeforeTax, Is.EqualTo(1m));
  32. Assert.That(product.QuantityInStock, Is.EqualTo(1));
  33. Assert.That(product.Weight, Is.EqualTo(1.0));
  34. }
  35. [Test]
  36. public void SettingTheValueOfAProperty()
  37. {
  38. var builderSetup = new BuilderSetup();
  39. var product = new Builder<Product>(builderSetup)
  40. .CreateNew()
  41. .With(x => x.Description = "A custom description here")
  42. .Build();
  43. Assert.That(product.Title, Is.EqualTo("Title1"));
  44. Assert.That(product.Description, Is.EqualTo("A custom description here"));
  45. Assert.That(product.Id, Is.EqualTo(1));
  46. }
  47. [Test]
  48. public void SettingMultipleProperties()
  49. {
  50. var builderSetup = new BuilderSetup();
  51. var product = new Builder<Product>(builderSetup)
  52. .CreateNew()
  53. .With(x => x.Title = "Special title")
  54. .And(x => x.Description = "Special description")
  55. .And(x => x.Id = 2)
  56. .Build();
  57. Assert.That(product.Title, Is.EqualTo("Special title"));
  58. Assert.That(product.Description, Is.EqualTo("Special description"));
  59. Assert.That(product.Id, Is.EqualTo(2));
  60. // NB: You can call With() multiple times too, but And() is provided
  61. // to improve readability
  62. }
  63. [Test]
  64. public void ItsPossibleToAssignValuesToPrivateSetProperties()
  65. {
  66. var builderSetup = new BuilderSetup();
  67. var invoice = new Builder<Invoice>(builderSetup)
  68. .CreateNew()
  69. .With(x => x.Amount, 100)
  70. .Build();
  71. Assert.That(invoice.Amount, Is.EqualTo(100));
  72. }
  73. [Test]
  74. public void ItsPossibleToAssignValuesToReadonlyProperties()
  75. {
  76. var builderSetup = new BuilderSetup();
  77. var invoice = new Builder<Invoice>(builderSetup)
  78. .CreateNew()
  79. .With(x => x.Id, 100)
  80. .Build();
  81. Assert.That(invoice.Id, Is.EqualTo(100));
  82. }
  83. [Test]
  84. public void ItsPosibleToUseAndInAdditionToWithInOrderToImproveReadability()
  85. {
  86. var builderSetup = new BuilderSetup();
  87. var invoice = new Builder<Invoice>(builderSetup)
  88. .CreateNew()
  89. .With(x => x.Amount, 100)
  90. .And(x => x.Id, 200)
  91. .Build();
  92. Assert.That(invoice.Amount, Is.EqualTo(100));
  93. Assert.That(invoice.Id, Is.EqualTo(200));
  94. }
  95. [Test]
  96. public void ItsPossibleToUseThePrivateSetWithToSetNormalProperties()
  97. {
  98. var builderSetup = new BuilderSetup();
  99. var product = new Builder<Product>(builderSetup)
  100. .CreateNew()
  101. .With(x => x.Title, "special title")
  102. .Build();
  103. Assert.That(product.Title, Is.EqualTo("special title"));
  104. }
  105. [Test]
  106. public void CreatingAClassThatHasAConstructorUsingLegacySyntax()
  107. {
  108. var builderSetup = new BuilderSetup();
  109. var basket = new Builder<ShoppingBasket>(builderSetup).CreateNew().Build();
  110. var product = new Builder<Product>(builderSetup).CreateNew().Build();
  111. const int quantity = 5;
  112. // BasketItem's ctor: BasketItem(ShoppingBasket basket, Product product, int quantity)
  113. var basketItem = new Builder<BasketItem>(builderSetup)
  114. .CreateNew()
  115. .WithConstructorArgs(basket, product, quantity)
  116. .Build();
  117. // The property namer will still apply sequential names to the properties
  118. // however it won't overwrite any properties that have been set through the constructor
  119. Assert.That(basketItem.DiscountCode, Is.EqualTo("DiscountCode1"));
  120. Assert.That(basketItem.Basket, Is.EqualTo(basket));
  121. Assert.That(basketItem.Product, Is.EqualTo(product));
  122. Assert.That(basketItem.Quantity, Is.EqualTo(quantity));
  123. }
  124. [Test]
  125. public void CreatingAClassThatHasAConstructorUsingExpressionSyntax()
  126. {
  127. var builderSetup = new BuilderSetup();
  128. var basket = new Builder<ShoppingBasket>(builderSetup).CreateNew().Build();
  129. var product = new Builder<Product>(builderSetup).CreateNew().Build();
  130. const int quantity = 5;
  131. // BasketItem's ctor: BasketItem(ShoppingBasket basket, Product product, int quantity)
  132. var basketItem = new Builder<BasketItem>(builderSetup)
  133. .CreateNew()
  134. .WithConstructor(() => new BasketItem(basket, product, quantity))
  135. .Build();
  136. // The property namer will still apply sequential names to the properties
  137. // however it won't overwrite any properties that have been set through the constructor
  138. Assert.That(basketItem.DiscountCode, Is.EqualTo("DiscountCode1"));
  139. Assert.That(basketItem.Basket, Is.EqualTo(basket));
  140. Assert.That(basketItem.Product, Is.EqualTo(product));
  141. Assert.That(basketItem.Quantity, Is.EqualTo(quantity));
  142. }
  143. [Test]
  144. public void UsingDo()
  145. {
  146. var builderSetup = new BuilderSetup();
  147. var child = new Builder<Category>(builderSetup).CreateNew().Build();
  148. var category = new Builder<Category>(builderSetup)
  149. .CreateNew()
  150. .Do(x => x.AddChild(child))
  151. .Build();
  152. Assert.That(category.Children[0], Is.EqualTo(child));
  153. }
  154. [Test]
  155. public void CallingMultipleMethods()
  156. {
  157. var builderSetup = new BuilderSetup();
  158. var child = new Builder<Category>(builderSetup).CreateNew().Build();
  159. var anotherChild = new Builder<Category>(builderSetup).CreateNew().Build();
  160. var category = new Builder<Category>(builderSetup)
  161. .CreateNew()
  162. .Do(x => x.AddChild(child))
  163. .And(x => x.AddChild(anotherChild))
  164. .Build();
  165. Assert.That(category.Children[0], Is.EqualTo(child));
  166. Assert.That(category.Children[1], Is.EqualTo(anotherChild));
  167. }
  168. [Test]
  169. [Description("Multi functions allow you to call a method on an object on each item in a list")]
  170. public void UsingMultiFunctions()
  171. {
  172. var builderSetup = new BuilderSetup();
  173. var categories = new Builder<Category>(builderSetup).CreateListOfSize(5).Build();
  174. var product = new Builder<Product>(builderSetup)
  175. .CreateNew()
  176. .DoForAll( (prod, cat) => prod.AddToCategory(cat), categories)
  177. .Build();
  178. // Assertions are intentionally verbose for clarity
  179. Assert.That(product.Categories.Count, Is.EqualTo(5));
  180. Assert.That(product.Categories[0], Is.EqualTo(categories[0]));
  181. Assert.That(product.Categories[1], Is.EqualTo(categories[1]));
  182. Assert.That(product.Categories[2], Is.EqualTo(categories[2]));
  183. Assert.That(product.Categories[3], Is.EqualTo(categories[3]));
  184. Assert.That(product.Categories[4], Is.EqualTo(categories[4]));
  185. }
  186. [Test]
  187. [ExpectedException(typeof(TypeCreationException))]
  188. public void NBuilderIsNotAMockingFramework() // (!)
  189. {
  190. var builderSetup = new BuilderSetup();
  191. new Builder<IProduct>(builderSetup).CreateNew().Build();
  192. // ^
  193. }
  194. [Test]
  195. public void NBuilderCannotBeUsedToBuildInterfaces()
  196. {
  197. var builderSetup = new BuilderSetup();
  198. var ex = Assert.Throws<TypeCreationException>(() => new Builder<IMyInterface>(builderSetup).CreateNew().Build());
  199. Assert.That(ex.Message, Is.EqualTo("Cannot build an interface"));
  200. }
  201. [Test]
  202. public void NBuilderCannotBeUsedToBuildAbstractClasses()
  203. {
  204. var builderSetup = new BuilderSetup();
  205. var ex = Assert.Throws<TypeCreationException>(() => new Builder<MyAbstractClass>(builderSetup).CreateNew().Build(), "Cannot build an abstract class");
  206. Assert.That(ex.Message, Is.EqualTo("Cannot build an abstract class"));
  207. }
  208. [Test]
  209. [ExpectedException(typeof(TypeCreationException))]
  210. public void WillComplainIfYouTryToBuildAClassThatCannotBeInstantiatedDirectly()
  211. {
  212. var builderSetup = new BuilderSetup();
  213. new Builder<ChuckNorris>(builderSetup).CreateNew().Build();
  214. }
  215. }
  216. }