Commits

Bogdan Varlamov  committed 554f465

updating the tests to cleanup the data context after each test to ensure integrity

  • Participants
  • Parent commits c76dfed

Comments (0)

Files changed (2)

File development/src/Tests/PostLogicTest.cs

         private ContentModelContainerMock _context;
 
         #region Additional test attributes
-        // 
-        //You can use the following additional attributes as you write your tests:
-        //
-        //Use ClassInitialize to run code before running the first test in the class
-        //[ClassInitialize()]
-        //public static void MyClassInitialize(TestContext testContext)
-        //{
-        //}
-        //
-        //Use ClassCleanup to run code after all tests in a class have run
-        //[ClassCleanup()]
-        //public static void MyClassCleanup()
-        //{
-        //}
-        //
 
+        /// <summary>
+        /// need to build up a clean data context prior to each test
+        /// </summary>
         [TestInitialize()]
         public void TestInitialize()
         {
             
         }
 
+        /// <summary>
+        /// clears out the changed DB after each test
+        /// so changes aren't persisted between tests
+        /// </summary>
+        [TestCleanup()]
+        public void MyTestCleanup()
+        {
+            //NOTE: this is messy, there are some extension methods to do this online
+            //maybe something like:
+
+            //public static void DeleleAllObjects<TEntity>( this ObjectSet<TEntity> objectSet,
+            //                                              IEnumerable<TEntity> objects)
+            //    where TEntity : class
+            //{
+
+            //   foreach(var o in objects)
+            //   {
+            //      objectSet.DeleteObject(o);
+            //   }
+            //}
+            //not sure if I want to start extending EF, looks like this will be coming in EF6 anyway
+            //maybe think about looping through the properties of the context that are object sets
+            //with reflection and deleting them that way?
+            foreach(var toDelete in _context.Posts.ToList())
+            {
+                _context.Posts.DeleteObject(toDelete);
+            }
 
-        //Use TestCleanup to run code after each test has run
-        //[TestCleanup()]
-        //public void MyTestCleanup()
-        //{
-        //}
-        //
+            foreach (var toDelete in _context.PostTypes.ToList())
+            {
+                _context.PostTypes.DeleteObject(toDelete);
+            }
+
+            foreach (var toDelete in _context.UserProfiles.ToList())
+            {
+                _context.UserProfiles.DeleteObject(toDelete);
+            }
+        }
+        
         #endregion
 
 

File development/src/Tests/Tests.csproj

   <ItemGroup>
     <None Include="App.config" />
     <None Include="packages.config" />
+    <Shadow Include="Test References\qandaNET.BLL.accessor" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it.