TDD, Architecture and Testing Code in Isolation :: Part 3

Navigate back to Part 2 of this series of entries.

Ok, ok, ok, the other two parts where lean on the unit tests.  Now we get down to the mocking, faking, and testing.  The first two tests I wrote have to do with the UserListing Entity.  Once again, since we’re in a situation were thing are generated by LINQ, I’m not really sure of a solid reason, or how, to write tests that fail first.  How would one do that for testing entities?  I’m not sure but here are my first two that run returning green lights.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using MbUnit.Framework;
   4:  using UnitTest.Controller;
   5:  using UnitTest.Mocks;
   6:   
   7:  namespace DataAccessLayer.Unit.mbUnitTests
   8:  {
   9:      [TestFixture]
  10:      public class UserListing
  11:      {
  12:          [Test]
  13:          public void TestGetUserListingEntity()7
  14:          {
  15:              DateTime theDate = DateTime.Now.AddDays(-6);
  16:              var controller = new TsrUserListingController
  17:                                   {
  18:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  19:                                   };
  20:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing(theDate);
  21:              Assert.IsNotNull(results);
  22:          }
  23:   
  24:          [Test]
  25:          public void TestGetUserListingEntityCount()
  26:          {
  27:              DateTime theDate = DateTime.Now.AddDays(-6);
  28:              var controller = new TsrUserListingController
  29:                                   {
  30:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  31:                                   };
  32:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing(theDate);
  33:   
  34:              int count = 0;
  35:              foreach (TsrUserListing tsrUserListing in results)
  36:              {
  37:                  count += 1;
  38:              }
  39:              Assert.IsTrue(count == 3);
  40:          }
  41:      }
  42:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

I guess I could write some tests to just throw the asserts, such as checking for a count that I know would be wrong, but why?  Anyone have notions on that I’d be keen to hear them because I honestly feel I’m missing something.  Then of course I am testing generated code, so maybe this is a reasonable expectation.  At this point I also made the same tests for the TmtUserListing since those classes would need the same tests for assurance.

First I had to add the fake tables and some mock objects to the table entities.  In the ExampleMockDatabase add the following TmtUserListing to the “CreateTables()” method.

   1:  protected override void CreateTables()
   2:  {
   3:      AddTable<TsrUserListing>();
   4:      AddTable<TmtUserListing>();
   5:  }Now that we have our table we can add the objects to it.  Add this code snippet to the end of the Populate method.
   6:   
   7:  var testTmtUserListing1 =
   8:      new TmtUserListing
   9:          {
  10:              UserId = Guid.NewGuid(),
  11:              UserName = "Test User", 
  12:              LoweredUserName = "test user",
  13:              IsAnonymous = false,
  14:              LastActivityDate = DateTime.Now.AddDays(-4),
  15:              MobileAlias = "test user"
  16:          };
  17:  var testTmtUserListing2 =
  18:      new TmtUserListing
  19:          {
  20:              UserId = Guid.NewGuid(),
  21:              UserName = "User Test",
  22:              LoweredUserName = "user test",
  23:              IsAnonymous = true,
  24:              LastActivityDate = DateTime.Now.AddMinutes(-23),
  25:              MobileAlias = "user test"
  26:          };
  27:  var testTmtUserListing3 =
  28:      new TmtUserListing
  29:          {
  30:              UserId = Guid.NewGuid(),
  31:              UserName = "John Doe",
  32:              LoweredUserName = "john doe",
  33:              IsAnonymous = true,
  34:              LastActivityDate = DateTime.Now.AddHours(-22),
  35:              MobileAlias = "john doe"
  36:          };
  37:   
  38:  GetTable<TmtUserListing>().Add(testTmtUserListing1);
  39:  GetTable<TmtUserListing>().Add(testTmtUserListing2);
  40:  GetTable<TmtUserListing>().Add(testTmtUserListing3);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

I “suppose” we could have written the tests first, but as I’ve mentioned since it is mostly generated code this just doesn’t seem as important to do.  I’m however, if you got em’, looking for reasons and ways to write legitimate red light tests against this first.

Next I built out a controller specific to the TmtUserListing Entities.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using DataAccessLayer;
   5:  using UnitTest.Interfaces;
   6:   
   7:  namespace UnitTest.Controller
   8:  {
   9:      public class TmtUserListingController
  10:      {
  11:          public IDataContextWrapper DataContext { get; set; }
  12:   
  13:          public IEnumerable<TmtUserListing> GetTmtUserListing(DateTime lastActivityDate)
  14:          {
  15:              IEnumerable<TmtUserListing> TmtUserListings = from TmtUserListing in DataContext.Table<TmtUserListing>()
  16:                                                            where TmtUserListing.LastActivityDate >= lastActivityDate
  17:                                                            select TmtUserListing;
  18:              return TmtUserListings;
  19:          }
  20:   
  21:          public IEnumerable<TmtUserListing> GetTmtUserListing()
  22:          {
  23:              IEnumerable<TmtUserListing> TmtUserListings = from TmtUserListing in DataContext.Table<TmtUserListing>()
  24:                                                            select TmtUserListing;
  25:              return TmtUserListings;
  26:          }
  27:      }
  28:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

…and of course the tests.  Which sure took long enough eh?

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using MbUnit.Framework;
   4:  using UnitTest.Controller;
   5:  using UnitTest.Mocks;
   6:   
   7:  namespace DataAccessLayer.Unit.mbUnitTests
   8:  {
   9:      [TestFixture]
  10:      public class UserListing
  11:      {
  12:          [Test]
  13:          public void TestGetTsrUserListingEntity()
  14:          {
  15:              DateTime theDate = DateTime.Now.AddDays(-6);
  16:              var controller = new TsrUserListingController
  17:                                   {
  18:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  19:                                   };
  20:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing(theDate);
  21:              Assert.IsNotNull(results);
  22:          }
  23:   
  24:          [Test]
  25:          public void TestGetTsrUserListingEntityCount()
  26:          {
  27:              DateTime theDate = DateTime.Now.AddDays(-6);
  28:              var controller = new TsrUserListingController
  29:                                   {
  30:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  31:                                   };
  32:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing(theDate);
  33:   
  34:              int count = 0;
  35:              foreach (TsrUserListing tsrUserListing in results)
  36:              {
  37:                  count += 1;
  38:              }
  39:              Assert.IsTrue(count == 3);
  40:          }
  41:   
  42:          [Test]
  43:          public void TestGetTsrUserListingAllEntity()
  44:          {
  45:              var controller = new TsrUserListingController
  46:                                   {DataContext = new MockDataContextWrapper(new ExampleMockDatabase())};
  47:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing();
  48:              Assert.IsNotNull(results);
  49:          }
  50:   
  51:          [Test]
  52:          public void TestGetTsrUserListingAllEntityCount()
  53:          {
  54:              var controller = new TsrUserListingController
  55:                                   {DataContext = new MockDataContextWrapper(new ExampleMockDatabase())};
  56:              IEnumerable<TsrUserListing> results = controller.GetTsrUserListing();
  57:   
  58:              int count = 0;
  59:              foreach (TsrUserListing tsrUserListing in results)
  60:              {
  61:                  count += 1;
  62:              }
  63:              Assert.IsNotNull(count == 3);
  64:          }
  65:   
  66:          [Test]
  67:          public void TestGetTmtUserListingEntity()
  68:          {
  69:              DateTime theDate = DateTime.Now.AddDays(-6);
  70:              var controller = new TmtUserListingController
  71:                                   {
  72:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  73:                                   };
  74:              IEnumerable<TmtUserListing> results = controller.GetTmtUserListing(theDate);
  75:              Assert.IsNotNull(results);
  76:          }
  77:   
  78:          [Test]
  79:          public void TestGetTmtUserListingEntityCount()
  80:          {
  81:              DateTime theDate = DateTime.Now.AddDays(-6);
  82:              var controller = new TmtUserListingController
  83:                                   {
  84:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
  85:                                   };
  86:              IEnumerable<TmtUserListing> results = controller.GetTmtUserListing(theDate);
  87:   
  88:              int count = 0;
  89:              foreach (TmtUserListing tmtUserListing in results)
  90:              {
  91:                  count += 1;
  92:              }
  93:              Assert.IsTrue(count == 3);
  94:          }
  95:   
  96:          [Test]
  97:          public void TestGetTmtUserListingAllEntity()
  98:          {
  99:              var controller = new TmtUserListingController
 100:                                   {
 101:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
 102:                                   };
 103:              IEnumerable<TmtUserListing> results = controller.GetTmtUserListing();
 104:              Assert.IsNotNull(results);
 105:          }
 106:   
 107:          [Test]
 108:          public void TestGetTmtUserListingAllEntityCount()
 109:          {
 110:              var controller = new TmtUserListingController
 111:                                   {
 112:                                       DataContext = new MockDataContextWrapper(new ExampleMockDatabase())
 113:                                   };
 114:              IEnumerable<TmtUserListing> results = controller.GetTmtUserListing();
 115:   
 116:              int count = 0;
 117:              foreach (TmtUserListing tmtUserListing in results)
 118:              {
 119:                  count += 1;
 120:              }
 121:              Assert.IsTrue(count == 3);
 122:          }
 123:      }
 124:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

We now have tests that assure us our Data Access Layer is getting and retrieving the appropriate entities etc. 

In forthcoming entries I'll be adding to this type of testing entry based on the current Assembler, Data Transfer Object, and Remote Facade Architecture that the team I'm working with has been building.  We're doing some interesting things that will provide us with an opportunity to really step into some oddball much needed unit test scenarios.

Also, if anyone actually reads my grand article - what's the best way to get a count of an IEnumerable list?  I've completely drawn a blank on what the best practice are for doing that.  Maybe I should return a Generics List back instead?  Blagh... anyway, a test is a test is a test - if it serves the function it will work.

References and extra info for this Triumvirate of Entries:

kick it on DotNetKicks.com