Mocking a base class method call with Moq

后端 未结 6 1801
不思量自难忘°
不思量自难忘° 2020-12-06 16:12

I am modifiying a class method which formats some input paramater dates which are subsequently used as params in a method call into the base class (which lives in another as

6条回答
  •  天涯浪人
    2020-12-06 16:33

    As of 2013 with latest Moq you can. Here is an example

    public class ViewModelBase
    {
        public virtual bool IsValid(DateTime date)
        {
            //some complex shared stuff here
        }
    } 
    
    public class MyViewModel : ViewModelBase
    {
        public void Save(DateTime date)
        {
            if (IsValid(date))
            {
                //do something here
            }
        }
    }
    
    public void MyTest()
    {
        //arrange
        var mockMyViewModel = new Mock(){CallBase = true};
        mockMyViewModel.Setup(x => x.IsValid(It.IsAny())).Returns(true);
    
        //act
        mockMyViewModel.Object.Save();
    
        //assert
        //do your assertions here
    } 
    

提交回复
热议问题