Commits

Thomas Weller  committed 60c42df

MODIFIED: Refactored TM fake data into a separate Helper class
CHANGED: SqlServer tests now use 'Rollback' attribute
CHANGED: conn string for Sql Server tests (now points to local db instance)

  • Participants
  • Parent commits 0cf31fe

Comments (0)

Files changed (8)

File CourseManager.Data/PersonRepository.cs

         {
             try
             {
-                return _entities.People.Select(@p => @p.GetFullName(nameOrdering))
-                                       .OrderBy(@name => @name)
-                                       .ToList();
+                var fullNames =  new List<string>(_entities.People
+                                                           .ToList() 
+                                                           .Select(@p => @p.GetFullName(nameOrdering)));
+               fullNames.Sort();
+
+               return fullNames;
             }
             catch (Exception exception)
             {

File CourseManager.Test.MsSqlServer/App.Config

 
 <configuration>
   <connectionStrings>
-    <add name="SchoolEntities" connectionString="metadata=res://*/School.csdl|res://*/School.ssdl|res://*/School.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=DB;Initial Catalog=School;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
+    <add name="SchoolEntities" 
+         connectionString="metadata=res://*/School.csdl|res://*/School.ssdl|res://*/School.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=DEV\SQLEXPRESS;Initial Catalog=School;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
   </connectionStrings>
 </configuration>

File CourseManager.Test.MsSqlServer/CourseManager.Test.MsSqlServer.csproj

     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
-    <None Include="App.Config" />
+    <None Include="App.Config">
+      <SubType>Designer</SubType>
+    </None>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

File CourseManager.Test.MsSqlServer/PersonRepositoryFixture.cs

         #region Tests
 // ReSharper disable InconsistentNaming
 
-        [Test, MultipleAsserts, TestsOn("PersonRepository.GetNameList")]
+        [Test, Rollback, MultipleAsserts, TestsOn("PersonRepository.GetNameList")]
         public void GetNameList_ListOrdering_ReturnsTheExpectedFullNames()
         {
             List<string> names =
             Assert.AreEqual("Zheng, Roger", names.Last());
         }
 
-        [Test, MultipleAsserts, TestsOn("PersonRepository.GetNameList")]
+        [Test, Rollback, MultipleAsserts, TestsOn("PersonRepository.GetNameList")]
         public void GetNameList_NormalOrdering_ReturnsTheExpectedFullNames()
         {
             List<string> names = 
         }
 
 
-        [Test, TestsOn("PersonRepository.AddPerson")]
+        [Test, Rollback, TestsOn("PersonRepository.AddPerson")]
         public void AddPerson_CalledOnce_IncreasesCountByOne()
         {
             int count = _personRepository.Count;
             Assert.AreEqual(count + 1, _personRepository.Count);
         }
 
-        [Test, TestsOn("PersonRepository.AddPerson")]
+        [Test, Rollback, TestsOn("PersonRepository.AddPerson")]
         public void AddPerson_CalledWithNull_DoesNothing()
         {
             int count = _personRepository.Count;
             Assert.AreEqual(count, _personRepository.Count);
         }
 
-        [Test, TestsOn("PersonRepository.RemovePerson")]
+        [Test, Rollback, TestsOn("PersonRepository.RemovePerson")]
         public void RemovePerson_CalledOnce_DecreasesCountByOne()
         {
             int count = _personRepository.Count;
 
-            _personRepository.RemovePerson(new Person { PersonID = 4 });
+            _personRepository.RemovePerson(new Person { PersonID = 33 });
 
             Assert.AreEqual(count - 1, _personRepository.Count);
         }
 
-        [Test, TestsOn("PersonRepository.RemovePerson")]
+        [Test, Rollback, TestsOn("PersonRepository.RemovePerson")]
         public void RemovePerson_CalledWithNull_DoesNothing()
         {
 
             Assert.AreEqual(count, _personRepository.Count);
         }
 
-        [Test, TestsOn("PersonRepository.RemovePerson")]
+        [Test, Rollback, TestsOn("PersonRepository.RemovePerson")]
         public void RemovePerson_ForWhomAnOfficeAssignmentExists_Throws()
         {
             Assert.Throws<RepositoryException>(() =>
                 _personRepository.RemovePerson(new Person { PersonID = 18 }));
         }
 
-        [Test, MultipleAsserts, TestsOn("PersonRepository.GetCourseMembers")]
+        [Test, Rollback, MultipleAsserts, TestsOn("PersonRepository.GetCourseMembers")]
         [Row(null, typeof(ArgumentNullException))]
         [Row("", typeof(ArgumentException))]
         [Row("NotExistingCourse", typeof(ArgumentException))]
             Assert.IsInstanceOfType(expectedInnerExceptionType, exception.InnerException);
         }
 
-        [Test, MultipleAsserts, TestsOn("PersonRepository.GetCourseMembers")]
+        [Test, Rollback, MultipleAsserts, TestsOn("PersonRepository.GetCourseMembers")]
         public void GetCourseMembers_WhenGivenAnExistingCourse_ReturnsListOfStudents()
         {
             List<Person> persons = _personRepository.GetCourseMembers("Macroeconomics");
 
-            Assert.Count(4, persons);
+            Assert.Count(6, persons);
             Assert.ForAll(
                 persons,
-                @p => new[] { 10, 11, 12, 14 }.Contains(@p.PersonID),
+                @p => new[] { 9, 10, 11, 12, 14, 22 }.Contains(@p.PersonID),
                 "Person has none of the expected IDs.");
         }
 

File CourseManager.Test.NDbUnit/PersonRepositoryFixture.cs

 
             int count = _personRepository.Count;
 
-            _personRepository.RemovePerson(new Person { PersonID = 4 });
+            _personRepository.RemovePerson(new Person { PersonID = 33 });
 
             Assert.AreEqual(count - 1, _personRepository.Count);
         }

File CourseManager.Test.Typemock/CourseManager.Test.Typemock.csproj

     <Reference Include="Typemock.ArrangeActAssert, Version=6.1.2.0, Culture=neutral, PublicKeyToken=3dae460033b8d8e2, processorArchitecture=MSIL" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="FakeDataHolder.cs" />
     <Compile Include="PersonRepositoryFixture.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

File CourseManager.Test.Typemock/FakeDataHolder.cs

+// ************************************************************************************************************************************************************
+// <copyright file="FakeDataHolder.cs" company="Thomas Weller Software-Entwicklung">
+//  Copyright (c) 2011, Thomas Weller Software-Entwicklung. All rights reserved.
+// </copyright>
+// <authors>
+//   <author>Thomas Weller</author>
+// </authors>
+// <license name="Simplified BSD License" url="http://www.freebsd.org/copyright/freebsd-license.html">
+//
+//    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+//    
+//       1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+//       2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the 
+//          documentation and/or other materials provided with the distribution.
+//    
+//    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
+//    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
+//    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
+//    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+//    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//    
+// </license>
+// <Description/>
+// ************************************************************************************************************************************************************
+
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+
+using CourseManager.Data;
+
+namespace CourseManager.Test.Typemock
+{
+    static class FakeDataHolder
+    {
+        #region Fields
+
+        // The xxxData collections serve as an internal cache. They represent the database,
+        // are filled during construction  and not altered thereafter.
+        private static ReadOnlyCollection<Person> _personData;
+        private static ReadOnlyCollection<OfficeAssignment> _officeAssignmentData;
+
+        // The xxxList collections mirror the xxxData collections. They represent a working copy of the data,
+        // which is exposed through properties to the outside and can be reset via the 'Reset()' method.
+        private static readonly List<Person> PersonList = new List<Person>();
+        private static readonly List<OfficeAssignment> OfficeAssignmentList = new List<OfficeAssignment>();
+
+        #endregion // Fields
+
+        #region Properties
+
+        public static IQueryable<Person> Persons
+        {
+            get { return PersonList.AsQueryable(); }
+        }
+
+        public static IQueryable<OfficeAssignment> OfficeAssignments
+        {
+            get { return OfficeAssignmentList.AsQueryable(); }
+        }
+
+        #endregion // Properties
+
+        #region Construction
+
+        static FakeDataHolder()
+        {
+            Setup();
+        }
+
+        #endregion // Construction
+
+        #region Operations
+
+        public static void Reset()
+        {
+            PersonList.Clear();
+            PersonList.AddRange(_personData);
+
+            OfficeAssignmentList.Clear();
+            OfficeAssignmentList.AddRange(_officeAssignmentData);
+        }
+
+        public static void AddPerson(Person person)
+        {
+            PersonList.Add(person);
+        }
+
+        public static void DeletePerson(Person person)
+        {
+            if (OfficeAssignmentList.Any(@a => @a.InstructorID == person.PersonID))
+            {
+                throw new InvalidOperationException("FK_OfficeAssignment_Person");
+            }
+
+            PersonList.Remove(person);
+        }
+
+        #endregion // Operations
+
+        #region Implementation
+
+        private static void Setup()
+        {
+            _personData = new List<Person>
+                {
+                    new Person {PersonID = 1, FirstName = "Kim", LastName = "Abercrombie"},
+                    new Person {PersonID = 2, FirstName = "Gytis", LastName = "Barzdukas"},
+                    new Person {PersonID = 3, FirstName = "Peggy", LastName = "Justice"},
+                    new Person {PersonID = 4, FirstName = "Fadi", LastName = "Fakhouri"},
+                    new Person {PersonID = 5, FirstName = "Roger", LastName = "Harui"},
+                    new Person {PersonID = 6, FirstName = "Yan", LastName = "Li"},
+                    new Person {PersonID = 7, FirstName = "Laura", LastName = "Norman"},
+                    new Person {PersonID = 8, FirstName = "Nino", LastName = "Olivotto"},
+                    new Person {PersonID = 9, FirstName = "Wayne", LastName = "Tang"},
+                    new Person {PersonID = 10, FirstName = "Meredith", LastName = "Alonso"},
+                    new Person {PersonID = 11, FirstName = "Sophia", LastName = "Lopez"},
+                    new Person {PersonID = 12, FirstName = "Meredith", LastName = "Browning"},
+                    new Person {PersonID = 13, FirstName = "Arturo", LastName = "Anand"},
+                    new Person {PersonID = 14, FirstName = "Alexandra", LastName = "Walker"},
+                    new Person {PersonID = 15, FirstName = "Carson", LastName = "Powell"},
+                    new Person {PersonID = 16, FirstName = "Damien", LastName = "Jai"},
+                    new Person {PersonID = 17, FirstName = "Robyn", LastName = "Carlson"},
+                    new Person {PersonID = 18, FirstName = "Roger", LastName = "Zheng"},
+                    new Person {PersonID = 19, FirstName = "Carson", LastName = "Bryant"},
+                    new Person {PersonID = 20, FirstName = "Robyn", LastName = "Suarez"},
+                    new Person {PersonID = 21, FirstName = "Roger", LastName = "Holt"},
+                    new Person {PersonID = 22, FirstName = "Carson", LastName = "Alexander"},
+                    new Person {PersonID = 23, FirstName = "Isaiah", LastName = "Morgan"},
+                    new Person {PersonID = 24, FirstName = "Randall", LastName = "Martin"},
+                    new Person {PersonID = 25, FirstName = "Candace", LastName = "Kapoor"},
+                    new Person {PersonID = 26, FirstName = "Cody", LastName = "Rogers"},
+                    new Person {PersonID = 27, FirstName = "Stacy", LastName = "Serrano"},
+                    new Person {PersonID = 28, FirstName = "Anthony", LastName = "White"},
+                    new Person {PersonID = 29, FirstName = "Rachel", LastName = "Griffin"},
+                    new Person {PersonID = 30, FirstName = "Alicia", LastName = "Shan"},
+                    new Person {PersonID = 31, FirstName = "Jasmine", LastName = "Stewart"},
+                    new Person {PersonID = 32, FirstName = "Kristen", LastName = "Xu"},
+                    new Person {PersonID = 33, FirstName = "Erica", LastName = "Gao"},
+                    new Person {PersonID = 34, FirstName = "Roger", LastName = "Van Houten"}
+                }
+                .AsReadOnly();
+
+            _officeAssignmentData = new List<OfficeAssignment>
+                {
+                    new OfficeAssignment { InstructorID = 18, Location = "143 Smith" }
+                }
+                .AsReadOnly();
+        }
+
+        #endregion // Implementation
+
+    } // class FakedataHolder
+
+} // namespace CourseManager.Test.Typemock

File CourseManager.Test.Typemock/PersonRepositoryFixture.cs

 
 using System;
 using System.Collections.Generic;
-using System.Collections.ObjectModel;
 using System.Linq;
 using CourseManager.Data;
 using MbUnit.Framework;
         private SchoolEntities _schoolContext;
         private PersonRepository _personRepository;
 
-        private ReadOnlyCollection<Person> _personData;
-        private readonly List<Person> _personList = new List<Person>();
-
-        private ReadOnlyCollection<OfficeAssignment> _officeAssignmentData;
-        private readonly List<OfficeAssignment> _officeAssignmentList = new List<OfficeAssignment>();
-
         #endregion // Fields
 
-        #region Setup/TearDown (fixture-wise)
-
-        [FixtureSetUp]
-        public void FixtureSetUp()
-        {
-            SetupFakeData();
-        }
+        #region Setup/TearDown
 
         [SetUp]
         public void SetUp()
             _personRepository = new PersonRepository(_schoolContext);
         }
 
-        #endregion // Setup/TearDown (fixture-wise)
+        #endregion // Setup/TearDown
 
         #region Tests
 // ReSharper disable InconsistentNaming
 
             int count = _personRepository.Count;
 
-            _personRepository.RemovePerson(new Person { PersonID = 4 });
+            _personRepository.RemovePerson(new Person { PersonID = 33 });
 
             Assert.AreEqual(count - 1, _personRepository.Count);
         }
             // Return the expected list of persons
             Isolate.WhenCalled(() => _schoolContext.People)
                    .WillReturnCollectionValuesOf(new List<Person>
-                                    {
-                                        new Person {PersonID = 10, FirstName = "Meredith", LastName = "Alonso"},
-                                        new Person {PersonID = 11, FirstName = "Sophia", LastName = "Lopez"},
-                                        new Person {PersonID = 12, FirstName = "Meredith", LastName = "Browning"},
-                                        new Person {PersonID = 14, FirstName = "Alexandra", LastName = "Walker"},
-                                    }
-                                    .AsQueryable());
+                                                    {
+                                                        new Person {PersonID = 9, FirstName = "Wayne", LastName = "Tang"},
+                                                        new Person {PersonID = 10, FirstName = "Meredith", LastName = "Alonso"},
+                                                        new Person {PersonID = 11, FirstName = "Sophia", LastName = "Lopez"},
+                                                        new Person {PersonID = 12, FirstName = "Meredith", LastName = "Browning"},
+                                                        new Person {PersonID = 14, FirstName = "Alexandra", LastName = "Walker"},
+                                                        new Person {PersonID = 22, FirstName = "Carson", LastName = "Alexander"}
+                                                    }
+                                                    .AsQueryable());
 
             List<Person> persons = _personRepository.GetCourseMembers("Macroeconomics");
 
-            Assert.Count(4, persons);
+            Assert.Count(6, persons);
             Assert.ForAll(
                 persons,
-                @p => new[] { 10, 11, 12, 14 }.Contains(@p.PersonID),
+                @p => new[] { 9, 10, 11, 12, 14, 22 }.Contains(@p.PersonID),
                 "Person has none of the expected IDs.");
         }
 // ReSharper restore AssignNullToNotNullAttribute
 
         #region Implementation
 
-        private void SetupFakeData()
-        {
-            _personData = new List<Person>
-                        {
-                                new Person {PersonID = 1, FirstName = "Kim", LastName = "Abercrombie"},
-                                new Person {PersonID = 2, FirstName = "Gytis", LastName = "Barzdukas"},
-                                new Person {PersonID = 3, FirstName = "Peggy", LastName = "Justice"},
-                                new Person {PersonID = 4, FirstName = "Fadi", LastName = "Fakhouri"},
-                                new Person {PersonID = 5, FirstName = "Roger", LastName = "Harui"},
-                                new Person {PersonID = 6, FirstName = "Yan", LastName = "Li"},
-                                new Person {PersonID = 7, FirstName = "Laura", LastName = "Norman"},
-                                new Person {PersonID = 8, FirstName = "Nino", LastName = "Olivotto"},
-                                new Person {PersonID = 9, FirstName = "Wayne", LastName = "Tang"},
-                                new Person {PersonID = 10, FirstName = "Meredith", LastName = "Alonso"},
-                                new Person {PersonID = 11, FirstName = "Sophia", LastName = "Lopez"},
-                                new Person {PersonID = 12, FirstName = "Meredith", LastName = "Browning"},
-                                new Person {PersonID = 13, FirstName = "Arturo", LastName = "Anand"},
-                                new Person {PersonID = 14, FirstName = "Alexandra", LastName = "Walker"},
-                                new Person {PersonID = 15, FirstName = "Carson", LastName = "Powell"},
-                                new Person {PersonID = 16, FirstName = "Damien", LastName = "Jai"},
-                                new Person {PersonID = 17, FirstName = "Robyn", LastName = "Carlson"},
-                                new Person {PersonID = 18, FirstName = "Roger", LastName = "Zheng"},
-                                new Person {PersonID = 19, FirstName = "Carson", LastName = "Bryant"},
-                                new Person {PersonID = 20, FirstName = "Robyn", LastName = "Suarez"},
-                                new Person {PersonID = 21, FirstName = "Roger", LastName = "Holt"},
-                                new Person {PersonID = 22, FirstName = "Carson", LastName = "Alexander"},
-                                new Person {PersonID = 23, FirstName = "Isaiah", LastName = "Morgan"},
-                                new Person {PersonID = 24, FirstName = "Randall", LastName = "Martin"},
-                                new Person {PersonID = 25, FirstName = "Candace", LastName = "Kapoor"},
-                                new Person {PersonID = 26, FirstName = "Cody", LastName = "Rogers"},
-                                new Person {PersonID = 27, FirstName = "Stacy", LastName = "Serrano"},
-                                new Person {PersonID = 28, FirstName = "Anthony", LastName = "White"},
-                                new Person {PersonID = 29, FirstName = "Rachel", LastName = "Griffin"},
-                                new Person {PersonID = 30, FirstName = "Alicia", LastName = "Shan"},
-                                new Person {PersonID = 31, FirstName = "Jasmine", LastName = "Stewart"},
-                                new Person {PersonID = 32, FirstName = "Kristen", LastName = "Xu"},
-                                new Person {PersonID = 33, FirstName = "Erica", LastName = "Gao"},
-                                new Person {PersonID = 34, FirstName = "Roger", LastName = "Van Houten"}
-                        }
-                .AsReadOnly();
-
-            _officeAssignmentData = new List<OfficeAssignment>
-                    {
-                            new OfficeAssignment { InstructorID = 18, Location = "143 Smith" }
-                    }
-                .AsReadOnly();
-        }
-
         private void FakeData()
         {
-            _personList.Clear();
-            _personList.AddRange(_personData);
-            _officeAssignmentList.Clear();
-            _officeAssignmentList.AddRange(_officeAssignmentData);
+            FakeDataHolder.Reset();
 
             // 'AsQueryable()' is necessary for possible Linq Extension Methods to work...
             Isolate.WhenCalled(() => _schoolContext.People)
-                    .WillReturnCollectionValuesOf(_personList.AsQueryable());
+                    .WillReturnCollectionValuesOf(FakeDataHolder.Persons);
             Isolate.WhenCalled(() => _schoolContext.People.AddObject(null))
-                   .DoInstead(@ctx => _personList.Add(@ctx.Parameters[0] as Person));
+                   .DoInstead(@ctx => FakeDataHolder.AddPerson(@ctx.Parameters[0] as Person));
             Isolate.WhenCalled(() => _schoolContext.People.DeleteObject(null))
-                   .DoInstead(@ctx => DeletePersonFromList(@ctx.Parameters[0] as Person));
-        }
-
-        private void DeletePersonFromList(Person person)
-        {
-            if (_officeAssignmentList.Any(@a => @a.InstructorID == person.PersonID))
-            {
-                throw new InvalidOperationException("FK_OfficeAssignment_Person");
-            }
-
-            _personList.Remove(person);
+                   .DoInstead(@ctx => FakeDataHolder.DeletePerson(@ctx.Parameters[0] as Person));
         }
 
         #endregion // Implementation