11

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?

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Attilah
  • 17,632
  • 38
  • 139
  • 202

4 Answers4

9

It looks like the only thing you are doing with the call to Begin() is returning your configured class for that particular interface: IUnitOfWork

You really just need to make sure that your call to Begin() returns a mock implementation of IUnitOfWork

One of two ways you can do this:

Option One - Refactor UnitOfWorkSS so that you can set the instance of IUnitOfWork to be returned

public static class UnitOfWorkSS  
{
    private static IUnitOfWork _unitOfWork;
    public static IUnitOfWork UnitOfWork
    {
        set { _unitOfWork = value; }
        private get{ _unitOfWork ?? (_unitOfWork = IoC.Resolve<IUnitOfWork>()); }
    }

    public static IUnitOfWork Begin()  
    {  
        return UnitOfWork;
    }  
}  

[TestMethod]
public void DoStuff()
{
    var mockUnitOfWork = new Mock<IUnitOfWork>();
    UnitOfWorkSS.UnitOfWork = mockUnitOfWork.Object;

    //Do some setup and verify
}

Option Two - Simply register a mock instance of IUnitOfWork with your IoC Container

private Mock<IUnitOfWork> _mockUnitOfWork;

[TestInitialize]
public void Init()
{
    _mockUnitOfWork = new Mock<IUnitOfWork>();

    //Making a lot of assumptions about your IoC here...
    IoC.Register<IUnitOfWork>(_mockUnitOfWork.Object);
}

[TestMethod]
public void DoStuff()
{
    _mockUnitOfWork.Setup( ... );

    //Do some verification
}
Community
  • 1
  • 1
Josh
  • 44,706
  • 7
  • 102
  • 124
  • Option 2 seems better to me considering option 1 introduces state into a static class only for the sake of making it testable - however maybe that's all the OP is looking for. – aaaaaa Aug 13 '17 at 15:05
2

Mock the IUnitOfWork and register it into your container so that it can be resolved.

Phill
  • 18,398
  • 7
  • 62
  • 102
1

I realize this is a very old question, but in case someone ends up here...

The best solution is a design change like the other answers say. However, if that's not possible, you can either use Microsoft Fakes (which replaced Moles) or, if you'd rather not depend on Visual Studio, there is a library called Smocks that can help.

https://github.com/vanderkleij/Smocks

1

As far as I know, you cannot mock static classes or methods.

Chris
  • 27,596
  • 25
  • 124
  • 225
  • 1
    yeah, i realized that. but the thing is that i need to fake that functionality, is there a way to do it ? – Attilah Nov 26 '10 at 03:58
  • You'd have to rewrite your unit of work stuff to be non-static, as far as I know. – Chris Nov 26 '10 at 04:00