The project that I have just joined uses the command pattern quite extensively for handling calls into the business logic layers of the project.
The business logic layers are built as static handlers calling into providers. The commands then call into these static handlers.
The team want to improve test coverage, and I would like us to move more toward true TDD but the use of static handlers means that there is a hard coded dependency in the commands and there is no way to inject the handlers as dependencies and so it is very difficult to unit test the commands in isolation.
Does anyone have any good suggestions for a strategy for us to move toward a more testable command pattern bearing in mind that there are a lot of commands already in use.
The process method below would be called when execute is called on the command. This would be the method under test. This is a typical example of the usage of static handlers in the command classes.
protected override bool Process()
{
this.user = SecurityHandler.ActivateUser(this.activationGuid);
bool success = this.user != null;
if (!success)
{
this.AddStatusMessage(StatusMessageType.Error, "/Commands/Security/ActivateUserCommand/IncorrectLink", true);
}
return success;
}
Static methods/fields generally hurt with testability – because it introduces the risk of tests not being isolated. One test could set some static field, changing the initial state for test2.
That said there was nothing in the Command Pattern that needed static handlers.. so I think it’s a custom implementation. I would like to see some sample code for a shorter answer.
There is no easy way out.. move the static parts into another class/object and pass it into the objects that need to access it. This might mean adding a parameter to methods that are currently using a TypeName.StaticField invocation to get at the data.
Update (for posted code snippet): I fail to see the Command Pattern here.. anyways to get the method under test.