This is a question that keeps coming up and I know if you're practicing it's a no brainer but I keep getting asked this by devs (I'm no testing God!).
The long answer is to read this book and pay attention when talking about 'inserting a seam'.
The short answer is carry on reading...
Now several people (read Jimmy Bogard) have already answered this but here is my take on this looking at my current client, they have a lots of deeply nested static dependencies - these are implicit dependencies and what you really want to is explicit dependencies because they are easily testable.
So I see a lot of classes like this nested deeply in some object graphs.
How do I make this easier to test and remove the hidden dependency?
The long answer is to read this book and pay attention when talking about 'inserting a seam'.
The short answer is carry on reading...
Now several people (read Jimmy Bogard) have already answered this but here is my take on this looking at my current client, they have a lots of deeply nested static dependencies - these are implicit dependencies and what you really want to is explicit dependencies because they are easily testable.
So I see a lot of classes like this nested deeply in some object graphs.
public class Foo
{
private string _url;
private string _connectionString;
private string _user;
public Foo()
{
_url = System.Configuration.ConfigurationManager.AppSettings["SomeUrl"];
_connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SomeConnectionString"].ConnectionString;
_user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
public void Bar()
{
// Do something...
}
}
So this has 2 static dependencies that are hidden and hard to test...How do I make this easier to test and remove the hidden dependency?
The answer is I insert a seam for each static dependency - this involves pushing the static dependency behind an interface and expose the properties & methods
So instead of using the ConfigurationManager and WindowsIdentity directly in the code we use an interface that's injected via the constructor.
public class Foo
{
private readonly IProvideConfiguration _configuration;
private readonly IProvideUserInfo _userInfo;
public Foo(IProvideConfiguration configuration, IProvideUserInfo userInfo)
{
_configuration = configuration;
_userInfo = userInfo;
}
public void Bar()
{
// Do something...
var user = _userInfo.UserName;
var url = _configuration.Url;
var connectionString = _configuration.ConnectionString;
}
}
public interface IProvideUserInfo
{
string UserName { get; }
}
public interface IProvideConfiguration
{
string Url { get; }
string ConnectionString { get; }
}
Now because I've created two seams - IProvideUserInfo & IProvideConfiguration these can easily be mocked out and testing becomes a lot easier!
So the implementation of these interface would probably look like this:
public class ApplicationConfiguration : IProvideConfiguration
{
public string Url
{
get { return System.Configuration.ConfigurationManager.AppSettings["SomeUrl"]; }
}
public string ConnectionString
{
get { return System.Configuration.ConfigurationManager.ConnectionStrings["SomeConnectionString"].ConnectionString; }
}
}
public class WindowsIdentity : IProvideUserInfo
{
public string UserName
{
get { return System.Security.Principal.WindowsIdentity.GetCurrent().Name; }
}
}
So hopefully some people will find this helpful....
Awkward Coder
Comments
Post a Comment