PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Mocking-with-Built-in-ObjectSet/EF_Tests/UnitTest1.cs

http://datacodecan.codeplex.com
C# | 222 lines | 77 code | 46 blank | 99 comment | 3 complexity | e08a84191e8c20cd51655be61d1704d3 MD5 | raw file
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System.Data.Objects;
  3. using EF_Model;
  4. using Moq;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace EF_Tests
  8. {
  9. [TestClass]
  10. public class UnitTest1
  11. {
  12. [TestMethod]
  13. public void TestMethod_Mock_Write()
  14. {
  15. //although no database interaction will be happened, but the corresponding web.config entry is needed
  16. Mock<DatabaseContext> dbContext = new Mock<DatabaseContext>();
  17. CustomerRepository cr = new CustomerRepository(dbContext.Object);
  18. //DatabaseContext dbContext = new DatabaseContext();
  19. //CustomerRepository cr = new CustomerRepository(dbContext);
  20. Customer c = new Customer();
  21. c.CustomerID = "008";
  22. c.ContactName = "xxx";
  23. c.CompanyName = "com-001";
  24. dbContext.Setup(db => db.ObjectSet<Customer>()).Returns(new FakeObjectSet<Customer>());
  25. /*
  26. Test method EF_Tests.UnitTest1.TestMethod_Mock_Write threw exception:
  27. System.NullReferenceException: Object reference not set to an instance of an object.
  28. To remove this error we need to declare public ObjectSet<Customer> Customers as non-virtual
  29. */
  30. //mock database write is being happened
  31. cr.AddCustomer(c);
  32. cr.SaveChanges();
  33. }
  34. [TestMethod]
  35. public void tmp1()
  36. {
  37. DatabaseContext dbContext2 = new DatabaseContext();
  38. var first = dbContext2.Customers.FirstOrDefault(c => c.CustomerID == "001");
  39. //var first = dbContext.Object.Customers.FirstOrDefault(c => c.CustomerID == "118");
  40. }
  41. [TestMethod]
  42. public void TestMethod_Mock_Read()
  43. {
  44. //although no database interaction will be happened, but the corresponding web.config entry is needed
  45. Mock<DatabaseContext> dbContext = new Mock<DatabaseContext>();
  46. CustomerRepository cr = new CustomerRepository(dbContext.Object);
  47. //DatabaseContext dbContext = new DatabaseContext();
  48. //CustomerRepository cr = new CustomerRepository(dbContext);
  49. Customer c = new Customer();
  50. c.CustomerID = "118";
  51. c.ContactName = "xxx-118";
  52. c.CompanyName = "com-001";
  53. //setting up the mock database
  54. ObjectSet<Customer> customerObjectSet = dbContext.Object.CreateObjectSet<Customer>();
  55. customerObjectSet.AddObject(c);
  56. /*
  57. Test method EF_Tests.UnitTest1.TestMethod_Mock_Read threw exception:
  58. System.ArgumentException: Invalid setup on a non-overridable member:
  59. db => db.Customers
  60. To remove this error we need to declare public ObjectSet<Customer> Customers as virtual
  61. */
  62. dbContext.Setup(db => db.Customers).Returns(customerObjectSet);
  63. //////////////////////////////
  64. //actual database read is being happened
  65. //???? why this code is hooking in the physical database, although we have mock setup for customer??
  66. //var first = dbContext.Object.Customers.FirstOrDefault(c1 => c1.CustomerID == "118");
  67. Customer first = cr.GetById("001");
  68. System.Console.WriteLine(first.ContactName);
  69. ///////////////////
  70. }
  71. [TestMethod]
  72. public void TestMethod_Mock_Read_With_Fake_Object()
  73. {
  74. //although no database interaction will be happened, but the corresponding web.config entry is needed
  75. Mock<DatabaseContext> dbContext = new Mock<DatabaseContext>();
  76. CustomerRepository cr = new CustomerRepository(dbContext.Object);
  77. //DatabaseContext dbContext = new DatabaseContext();
  78. //CustomerRepository cr = new CustomerRepository(dbContext);
  79. Customer c = new Customer();
  80. c.CustomerID = "118";
  81. c.ContactName = "xxx-118";
  82. c.CompanyName = "com-001";
  83. //setting up the mock database
  84. //ObjectSet<Customer> customerObjectSet = dbContext.Object.CreateObjectSet<Customer>();
  85. //customerObjectSet.AddObject(c);
  86. //FakeObjectSet<Customer> customerObjectSet = new FakeObjectSet<Customer>();
  87. //customerObjectSet.AddObject(c);
  88. var customers = new List<Customer> { c };
  89. var customerObjectSet = new Mock<FakeObjectSet<Customer>>(customers);
  90. /*
  91. Test method EF_Tests.UnitTest1.TestMethod_Mock_Read threw exception:
  92. System.ArgumentException: Invalid setup on a non-overridable member:
  93. db => db.Customers
  94. To remove this error we need to declare public ObjectSet<Customer> Customers as virtual
  95. */
  96. //dbContext.Setup(db => db.Customers).Returns(customerObjectSet);
  97. //dbContext.Setup(db => db.Customers).Returns(customerObjectSet.Object);
  98. dbContext.Setup(db => db.ObjectSet<Customer>()).Returns(customerObjectSet.Object);
  99. //////////////////////////////
  100. IObjectSet<Customer> customerObjectSet1 = dbContext.Object.Customers;
  101. Customer cA = customerObjectSet1.Single(c1 => c1.CustomerID == "118");
  102. System.Console.WriteLine(cA.ContactName);
  103. //////////////////////////////
  104. //actual database read is being happened
  105. //???? why this code is hooking in the physical database, although we have mock setup for customer??
  106. //var first = dbContext.Object.Customers.FirstOrDefault(c1 => c1.CustomerID == "118");
  107. //Customer first = cr.GetById("001");
  108. //System.Console.WriteLine(first.ContactName);
  109. ///////////////////
  110. }
  111. [TestMethod]
  112. public void TestMethod_Mock_Read_With_Fake_Object_2()
  113. {
  114. //although no database interaction will be happened, but the corresponding web.config entry is needed
  115. Mock<DatabaseContext> dbContext = new Mock<DatabaseContext>();
  116. CustomerRepository cr = new CustomerRepository(dbContext.Object);
  117. //DatabaseContext dbContext = new DatabaseContext();
  118. //CustomerRepository cr = new CustomerRepository(dbContext);
  119. Customer c = new Customer();
  120. c.CustomerID = "118";
  121. c.ContactName = "xxx-118";
  122. c.CompanyName = "com-001-xx";
  123. //setting up the mock database
  124. //ObjectSet<Customer> customerObjectSet = dbContext.Object.CreateObjectSet<Customer>();
  125. //customerObjectSet.AddObject(c);
  126. //FakeObjectSet<Customer> customerObjectSet = new FakeObjectSet<Customer>();
  127. //customerObjectSet.AddObject(c);
  128. /*var customers = new List<Customer> { c };
  129. var customerObjectSet = new Mock<FakeObjectSet<Customer>>();
  130. customerObjectSet.Object.AddObject(c);*/
  131. //FakeObjectSet<Customer> customerObjectSet = new FakeObjectSet<Customer>();
  132. //IObjectSet<Customer> customerObjectSet = dbContext.Object.ObjectSet<Customer>();
  133. /*
  134. Test method EF_Tests.UnitTest1.TestMethod_Mock_Read threw exception:
  135. System.ArgumentException: Invalid setup on a non-overridable member:
  136. db => db.Customers
  137. To remove this error we need to declare public ObjectSet<Customer> Customers as virtual
  138. */
  139. //dbContext.Setup(db => db.Customers).Returns(customerObjectSet);
  140. /*ObjectSet<Customer> customerObjectSet = dbContext.Object.CreateObjectSet<Customer>();
  141. customerObjectSet.AddObject(c);//really not adds anything!! this may because the dbContext is a mock object. To add it a concrete implementation is needed for dbContext
  142. dbContext.Setup(db => db.Customers).Returns(customerObjectSet);*/
  143. dbContext.Object.Customers.AddObject(c);
  144. //////////////////////////////
  145. IObjectSet<Customer> customerObjectSet1 = dbContext.Object.Customers;
  146. Customer cA = customerObjectSet1.Single(c1 => c1.CustomerID == "118");
  147. System.Console.WriteLine(cA.ContactName);
  148. //////////////////////////////
  149. //actual database read is being happened
  150. //???? why this code is hooking in the physical database, although we have mock setup for customer??
  151. //var first = dbContext.Object.Customers.FirstOrDefault(c1 => c1.CustomerID == "118");
  152. //Customer first = cr.GetById("001");
  153. //System.Console.WriteLine(first.ContactName);
  154. ///////////////////
  155. }
  156. //[TestMethod]
  157. //public void TestMethod_Mock_Read_With_Fake_ObjectSet()
  158. //{
  159. // //although no database interaction will be happened, but the corresponding web.config entry is needed
  160. // Mock<DatabaseContext> dbContext = new Mock<DatabaseContext>();
  161. // CustomerRepository cr = new CustomerRepository(dbContext.Object);
  162. // //DatabaseContext dbContext = new DatabaseContext();
  163. // //CustomerRepository cr = new CustomerRepository(dbContext);
  164. // Customer c = new Customer();
  165. // c.CustomerID = "118";
  166. // c.ContactName = "xxx-118";
  167. // c.CompanyName = "com-001";
  168. // //setting up the mock database
  169. // List<Customer> customers = new List<Customer> { c };
  170. // Mock<FakeObjectSet<Customer>> customerObjectSet = new Mock<FakeObjectSet<Customer>>(customers);
  171. // //dbContext.Setup(db => db.Customers).Returns(customerObjectSet.Object);
  172. // //actual database read is being happened
  173. // //System.Console.WriteLine(cr.GetById("118").ContactName);
  174. //}
  175. }
  176. }