Thursday, February 15, 2007

They forgot about me...

I'm busy writing today... too many meeting distractions...

I decided to check in with the Leukemia and Lymphoma Society today since I haven't heard about when training for my century ride was starting. And it turns out it started 2 weeks ago! I'm 20 miles behind :(

I'm a little bummed, but am ready to start and really don't want this to keep me from accomplishing my goal of riding a bike 100 miles and raising money for the Leukemia and Lymphoma society.

You can help me reach my goals here.

Wish me luck!!

Code Camp 2007

Looks like I'll be giving 2 presentations at Code Camp 2007 in NYC with a fellow developer from Oxygen, Oksana Udovitska. We're hosting two talks, The Gentle Art of Pair Programming and Testing in C# with RhinoMocks. Hopefully, people will learn something or at least be entertained. I'm thinking a code session taught by two hot chicks will be enough! hahaha

Code camp is in midtown on March 3rd, which is a Saturday (yeah, totally sucks, no?) and its from 9 - 6 or something like that...

Luke Melia -- another excellent developer on our team is also hosting a chat about leveraging the WPF command pattern with dependency injection. It should be good!

base test class

one thing that can make your tests real messy, real fast is duplicated code. however, with mocks, its really hard to get rid of duplication and keep your code readable.

a simple first step to uniform tests is a base test class that you inherit from in all your classes. use this class to setup your mock repository, replay, verify, tear down and do any custom setup your application needs for all your tests.



[TestFixture]
public void BaseTest
{
protected MockRepository _mocks;


[SetUp]
public virtual void Setup()
{
_mocks = new MockRepository();
}

[TearDown]
public virtual void TearDown()
{
}

protected virtual void FinalizeAll()
{
_mocks.ReplayAll();
}

protected void CreateMock<T>()
{
return _mocks.CreateMock<T>();
}

protected T XamlSerializeAndDeserialize<T>(T original)
{
using (MemoryStream stream = new MemoryStream())
{
XamlWriter.Save(original, stream);
stream.Position = 0;
return (T)XamlReader.Load(stream);
}
}
}


We keep the MockRepository protected because there are so many methods on it that inheritors may want to use, but use the base tests methods usages of _mocks when they exist. Also, we call the method that ReplaysAll "FinalizeAll" because methods that override often do more than just replay the mocks, they finalize the state of the other objects in the test. However, this may not be the best name, considering what "finalize" means in the c# world...

If you use an object management framework, logging or other common mocks, the base class can take the responsibility of setting those up.

As you can see, we added a method, "XamlSerializeAndDeserialize," since we test this in many of our classes, you could put this in a separate Assert class too, but it really depends on how often its used and what your team likes.

As your tests evolve, factories to create common mock objects and classes to do custom Asserts are also really helpful. One test class we've found really helpful is a PropertyChangedEventTester that will hook into any class that implements INotifyPropertyChanged and keep track of events.

all about the mocks

i love mocks. i really do. if i could, i would marry mocks, have lots of mock babies, be a grandmock; you get the idea...

the top reasons i love mocks:
1. focus only on what you are testing
2. other classes won't break your tests
3. create new interfaces and use them before they exist
4. use functionality without knowledge
5. i hate math
6. keep the file system, networks and other real resources out of your unit tests

i'll post examples of each of these reasons

a lot of black box testers have valid reasons for disliking mocks, but the benefits surpass any downside i have ever read or encountered. i use rhino mocks, which uses reflection, so some major downsides of mocks, such as difficult refactoring and type casting are completely avoided.

did i mention i love mocks?

thank you rhino mocks!!!

One other must with mocks is dependency injection. In the past I've written my own object management framework to handle injection, but we've been using the castle framework and its totally excellent, i love this too!!! It takes away all the thinking of dependency injection and lets you focus only on your tests and interfaces. An absolute must!!

Tuesday, February 13, 2007

if you don't need the exception...

don't declare a variable...

try
{
some code...
}
catch (ExceptionIWantToCatch)
{
}

No warnings, no cheesy statements to get rid of warnings...

If you don't care about the exception type, just catch{}

Monday, February 12, 2007

i just can't do it...

Code without resharper and visual assist that is...

After 10 minutes of my first Xna tutorial, I decided my first task would be to do all my developing in team edition.

Being my own customer, I made up the following criteria:
1. I can create a new c# library in vs team edition that contains a Game class which can be debugged/run from vs team edition
2. No post build copying of project output
3. No need for maintaining 2 separate solutions for each game
4. No dependencies between my game projects

In order to fulfill these, I created a new Xna solution called GameShell. I deleted the Game class, leaving Program.cs which I filled in with this code:
static void Main(string[] args)
{
string assemblyFilename = args[0];
string path = args[1];

Environment.CurrentDirectory = path;
Assembly assembly = Assembly.LoadFile(Path.Combine(path, assemblyFilename));
string typeName = args[2];
Game game = (Game)assembly.CreateInstance(typeName);
game.Run();
}

I used Assembly.LoadFile, because using Type.GetType(gameTypeName) was returning null... I don't think anything was spelled wrong, but I was feeling lazy, so I used the code above...

Its sad to admit, but before I thought to set Environment.CurrentDirectory, I had created a new appdomain that was rooted in the other directory and had a lovely class, GameStarter : MarshalByRef, that created and ran the game, which I thought was very clever, but completely unnecessary)


Now that I had GameShell to execute my game, I needed a game to run. I created a c# library in Team Edition, added references to Microsoft.Xna.Framework and Microsoft.Xna.Game and then added a game class to the project. Actually its Game1 from the first tutorial from the documentation (very exciting). Now that the project was ready to debug/run, I went into the project properties and changed the Debug settings so that it ran GameShell.exe with the proper command line arguments (AgileSolutions.GameLib.dll, FullPathToDebugDirectory, AgileSolutions.GameLib.Game1)

Viola! The game ran and I can now write any game library with my favorite tools and run it without any headaches... one minor issue though, graphics... you know, not the most important part of the video game, right?

For this first project, I cheated and put my graphics in the GameShell assembly, a big no no, but I'm gonna do some reading and learn more about content pipeline assets and we'll see what happens. Any ideas?

Wednesday, February 7, 2007

XNA

In addition to this very exciting blog, another goal for the year is to experiment with a new technology. Originally I was going to play around with Ruby, but I've decided to try out XNA instead. I was all psyched and ready to embark on the journey to creating my first xbox game when I discovered the following:

1. XNA development is only supported in VS Express addition!!! This is most upsetting because I cannot work without resharper and visual assist. Really, I can't. I keep doing things like ctrl-b, ctrl-alt-F7, ctrl-alt-v, shift F6, the list goes on... And intellisense without the ability to misspell just does not work well for me... I need colored listboxes!!!

2. No xbox live support :( this wasn't as much of a downer as #1, but I was looking forward to making up some clever achievements

3. How am I to test drive without add-ins? This really goes with #1, but this isn't about making development easier, this is about how I develop.

Despite the above, I'm still excited about my game idea and I hope its fun!

Wish me luck