July 2004 - Posts

TDD Q&A at the Capital Area .NET User Group
Wednesday, July 28, 2004 6:22 PM

Many thanks to the Capital Area .NET UG for their hospitality last night.  During my presentation on “TDD with ASP.NET” several interesting questions were raised and I have decided to repeat and answer them here (in no particular order):

  • When should you use a mock object?  What about mocking databases?

You should use a mock object in lots of different situations such as where the interaction is with an external system, or requires complex setup.  It is also useful where you need to closely test the interactions between certain objects.  In my work, I typically mock authentication systems (such as FormsAuthentication), messaging systems (SMTP) or other difficult to test systems.

However, I don't mock databases. 

Many people in the community (and the published TDD books) talk about mocking out the database functionality.  While this sounds great in principle, this doesn't seem to follow with the mainstream of *current* .NET development.  Many .NET developers are still using DataReaders and DataSets and tying their data access directly to their business logic - or even worse ... directly to their UI.  Then there is another generation (2nd?) which are using business objects to represent data and business logic - these objects manage their own persistence and several code generators exist (CodeSmith, etc) to generate such code.  The next generation (3rd?) seems to be some sort of persistence manager (such as Java Data Objects in the Java world) which we should see in the next .NET release with ObjectSpaces.

Update (8/4/2004):  I was just recently informed by Brian Noyes that ObjectSpaces is not expected to be in the 2.0 release but may be released shortly afterwards as an add-on.

So, where does all of this leave us with mocking databases?  Being able to mock the database depends on pulling out the functionality for persistence and retrieval to one place (once and only once!) and then replacing that one place with a mock object - with current practices in .NET this is not easily done.  Sure, you could roll your own persistence manager to just get and save objects but what about complex SQL queries and multiple joins or large complex searches?  This is the realm of the typical corporate developer (we live and breathe databases/SQL) so a little mock object is just not going to cut it.

I use a unittest database with test data - it isn't always pretty (or fast) but it works.

  • Why use “virtual” in the method signature when creating test cases?

Inheriting test cases for common functionality across different scenarios can be a very powerful way to test your application.  Using virtual makes such inheritance and customizing within your TestFixtures much easier.  I am also partial to virtual methods due to my Java background so I always use virtual methods as I hate the bugs that occur when methods are resolved by the type of the reference being used.

  • What built-in support is there for asynchronous testing in NUnit?

There are no built-in abilities for doing such things and I can't think how it would be used if there were.  However, tests are written in code (C# or whatever your .NET language of preference) and you can easily do anything that you need to do in certain situation such as launch new threads, etc.

(See this thread on the TDD Mailing List for more ideas on the topic of Asynchronous testing)

  • How does TDD fit in with traditional unit testing in a strong QA environment?

In my experiences in traditional environments, unit testing is either not done or done after the fact (and often poorly) by the same developers who wrote the code.  In this situation, TDD might replace such unit testing as it is being conducted by the same people and it is likely to be of higher quality if the TDD rules are being followed.  If you have a separate QA team who would normally test the codebase using their own techniques and tools then let them keep doing what they are already doing.  TDD is meant to provide the developer with a way to verify their code during development and provide a process for the design/development cycle - it is not meant to be a replacement for the QA process.

  • What will change with the launch of Visual Studio.NET 2005 with respect to the .NET TDD tools available today?

VS.NET 2005 has a built in unit testing tool which is apparently very NUnit-like.  There is also built-in coverage analysis which highlights the areas of code in red/green in the IDE showing the presence and absence of coverage.  I believe that there may also be the ability to add “filters“ to the source control system in VS.NET Team System enforcing unit tests to pass for checkin, etc.  There has also been some discussion on why the unit testing tools are only being included in the enterprise level tools.  I hope to learn more about these features as I play with the beta release.

Over all, the session was successful but was very pressed for time.  Scott Lock and Brian Noyes have asked me to return in a few months for Part 2 which will be delving further into the TDD coding sample, coverage analysis and so on!

WSE 2.0 at the DC .NET UG
Tuesday, July 20, 2004 11:00 PM

Todd Barr presented on Web Service Architecture and WSE 2.0 at the DC .NET UG tonight.  The session was very interesting especially seeing the new features involving security and policy.

We have just completed the web service phase of a project at my current client.  It involved a challenging problem that was happily solved using an old MSDN article by Aaron Skonnard.

The problem:

Our web service takes in a large complex data structure which has various business rules regarding the validity of the data.  If we begin using our business layer objects to create the corresponding data and child objects then we run the risk of violating a business rule which would throw an exception and then leave parent objects lying around in the database.  Yuck! 

The solution:

We first developed a schema which expressed all the data limitations but we needed this to fire before we attempted the business layer logic.  We could have simply accepted a string of XML as input to our WebMethod and then validated it against the schema but that seems so archaic in a world of generated web references which rich client proxy objects representing web service input and output. Aaron's article (link above) describes using a SoapExtensionAttribute and SoapExtension to enable schema validation of WebMethod input parameters.  This allows us to ensure that the input data meets the schema before even attempting processing by the business layer.  

Note that this validation against schema incurs a performance penalty which could be problematic for web services experiencing very heavy load.  There are “XML firewall” hardware appliances that can be used to validate XML web service input against schema in such extreme circumstances.  Mark O'Neill's book Web Service Security mentions such hardware solutions and also gives a good discussion on security best practices for web services.

It appears that policy may be an alternate solution provided by WSE 2.0 for the above problem although I have yet to dabble with it. :-D

by thycotic | with no comments
Filed under: ,
Pair Programming - use a "pair" Windows login?
Friday, July 16, 2004 9:11 AM

On my current consulting gig, we have been Pair Programming for almost all development.  In the past I have always just used one of the pair's Windows accounts while working.  This has proven to be a pain due to MSN Messenger, Email and other distractions typically being run in that account.  This also combined with one of us forgetting to check everything in and staying logged in on the pairing machine - preventing anything from happening till we arrive in the morning.

The solution:

A separate “pair” Windows Account on the pairing machine with a password known to the developers.  Can you feel the system administrators shaking? :-)  Fortunately, the company is small and we are able to control the creation of accounts, passwords, etc.

Our “Programming Pair 1“ login has the following configuration:

  • MSN Messenger login with only development team members added as contacts
  • Visual Studio.NET with our favorite keyboard shortcuts configured
  • NUnit, NUnitAddIn (who could live without it!)
  • Sourcegear's Vault client configured to use a pairing login
  • VNC Server for sharing keyboard and mouse
  • Other favorite utilities: Regulator, Reflector

How do you typically configure your pairing machines?

How would you handle these problems in a larger company where
stricter account management policies may be in place?

More Posts

This Blog

Syndication