Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”?

前端 未结 6 1607
野的像风
野的像风 2020-11-27 12:44

I have a unit test where I have to mock a non-virtual method that returns a bool type

public class XmlCupboardAccess
{
    public bool IsDataEntityInXmlCupbo         


        
6条回答
  •  忘掉有多难
    2020-11-27 13:17

    Instead of mocking concrete class you should mock that class interface. Extract interface from XmlCupboardAccess class

    public interface IXmlCupboardAccess
    {
        bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null);
    }
    

    And instead of

    private Mock _xmlCupboardAccess = new Mock();
    

    change to

    private Mock _xmlCupboardAccess = new Mock();
    

提交回复
热议问题