In my unit tests, I use things like AssemblyInitialize
, ClassInitialize
and TestInitialize to configure my tests. In AssemblyInitialize
I initialize some singleton factories for creating services, a unit of work and repositories (all trough Unity/Dependency Injection). In my TestInitialize I clear the state they have to make sure each test can run independently.
Trough Stackoverflow I came around the following articles: Writing Testable Code and How to Think About the “new” Operator with Respect to Unit Testing. I have to say they changed how I think about some code I've written but my mind is still buzzing.
For example, take the following code:
public class MyPresenter : BasePresenter<IMyView>
{
public MyPresenter(IMyView view)
: base(view)
{
}
public void PrepareView()
{
using (IMyService service = ServiceFactory.Instance.CreateService<IMyService>())
{
View.Data = service.GetData();
}
}
}
Which is used in an aspx page like this:
Presenter = new MyPresenter(this);
if (!IsPostback)
{
presenter.PrepareView();
}
How should I change this type of kind with the previous articles in mind? Should I pass a service instance to the PrepareView method? But then my ASPX page has to know about services and factories and stuff and dispose of the service after it's used.
What should I do with my Singleton factory? Make it a property of the class and inject it trough Unity?