I have these classes:
public static class UnitOfWorkSS
{
public static IUnitOfWork Begin()
{
return IoC.Resolve<IUnitOfWork>();
}
}
public class PostService
{
using (IUnitOfWork unitOfWork = UnitOfWorkSS.Begin())
{
//don't forget to sanitize html content
htmlContent = _htmlSanitizer.Sanitize(htmlContent);
IPost post = _factory.CreatePost(byUser, title, htmlContent);
_postRepository.Add(post);
unitOfWork.Commit();
}
}
How can I mock the classes UnitOfWorkSS and unitOfWork?
It looks like the only thing you are doing with the call to Begin() is returning your configured class for that particular interface:
IUnitOfWorkYou really just need to make sure that your call to Begin() returns a mock implementation of
IUnitOfWorkOne of two ways you can do this:
Option One – Refactor
UnitOfWorkSSso that you can set the instance ofIUnitOfWorkto be returnedOption Two – Simply register a mock instance of
IUnitOfWorkwith your IoC Container