Nothin but .NET - Tips and Tricks - Day 3

Coffee, coffee, coffee. Oh we need coffee. Okay, got it now we're ready. It's been a crazy week and it's getting crazier. JP is coding at about 8,000 words and using about 40,000 ReSharper shortcuts per minute now. He's also talking at about 10,000 words a minute. If you blur your eyes when looking at him, he begins starts to look like Neo in the Matrix. There is no mouse.

Service Layers are Tasks

I really like this paradigm of naming the service layer classes. Rather than having something like CustomerService.GetAllCustomers(), it's named CustomerTasks.GetAllCustomers(). I've always used the ServiceXXX naming strategy but it makes more sense to name things as tasks because in reality, they are tasks. Tasks in the service layer to serve up information to external consumers. It's about readability so choose whatever works for you.

Recording Ordered Mocks

We were working on the database today (gasp) but started mocking out a data gateway. I found it amusing that we couldn't record an ordered mock (but there was a way).

Hey Oren, check this out (in case you didn't know). This block fails:

   54 [Test]

   55 public void Should_be_able_to_get_a_datatable_from_the_database_ordered_failure()

   56 {

   57     using (mockery.Ordered())

   58     {

   59         using (mockery.Record())

   60         {

   61             Expect.Call(mockFactory.Create()).Return(mockConnection);

   62             Expect.Call(mockConnection.CreateCommandForDynamicSql("Blah")).Return(mockCommand);

   63             SetupResult.For(mockCommand.ExecuteReader()).Return(mockReader);

   64             mockConnection.Dispose();

   65         }

   66 

   67         using (mockery.Playback())

   68         {

   69             DataTable result = CreateSUT().GetADataTableUsing("Blah");

   70             Assert.IsNotNull(result);

   71         }

   72     }

   73 }

The error is:

[failure] DBGatewayTest.Setup.Should_be_able_to_get_a_datatable_from_the_database_ordered_failure.TearDown
TestCase 'DBGatewayTest.Setup.Should_be_able_to_get_a_datatable_from_the_database_ordered_failure.TearDown'
failed: Can't start replaying because Ordered or Unordered properties were call and not yet disposed.
    System.InvalidOperationException
    Message: Can't start replaying because Ordered or Unordered properties were call and not yet disposed.
    Source: Rhino.Mocks

In this case, the mock objects were created from interfaces that inherited from IDisposable. However it was a simple change to put the ordered call inside the record (because we don't care about ordering on playback). This slight change now works:

   33 [Test]

   34 public void Should_be_able_to_get_a_datatable_from_the_database()

   35 {

   36     using (mockery.Record())

   37     {

   38         using (mockery.Ordered())

   39         {

   40             Expect.Call(mockFactory.Create()).Return(mockConnection);

   41             Expect.Call(mockConnection.CreateCommandForDynamicSql("Blah")).Return(mockCommand);

   42             SetupResult.For(mockCommand.ExecuteReader()).Return(mockReader);

   43             mockConnection.Dispose();

   44         }

   45     }

   46 

   47     using (mockery.Playback())

   48     {

   49         DataTable result = CreateSUT().GetADataTableUsing("Blah");

   50         Assert.IsNotNull(result);

   51     }

   52 }

Just in case you ever needed to do have run into this problem one day.

More Nothin

JP is doing this 5-day intensive course that stretches people to think outside the box of how they typically program and delve into realms they have potentially not event thought about yet, about one per month. For the next little while (he's a busy dude) he's going to be travelin' dude. In October he'll be in New York City. In September he'll be in London, England and other locations are booked up into the new year.

If you want to find out more details about the course itself, check out the details here and be sure to get on board with the master.

2 Comments

Comments have been disabled for this content.