How to moq a static class with a static method (UnitOfWork case)?

后端 未结 4 2051
無奈伤痛
無奈伤痛 2021-02-08 02:10

I have these classes:

public static class UnitOfWorkSS 
{
  public static IUnitOfWork Begin()
  {
    return IoC.Resolve();
  }
}

public clas         


        
相关标签:
4条回答
  • 2021-02-08 02:29

    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
    }
    
    0 讨论(0)
  • 2021-02-08 02:47

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

    0 讨论(0)
  • 2021-02-08 02:50

    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

    0 讨论(0)
  • 2021-02-08 02:52

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

    0 讨论(0)
提交回复
热议问题