无法覆盖的成员上的设置无效:

时间:2013-10-12 12:36:01

标签: unit-testing mocking tdd virtual

我正在尝试使用Mock库测试函数GetcallEntityBycallEntity(Guid callId)。

此函数调用另一个未实现任何接口的类。

  

受保护的虚拟CallFacade CallFacade()          {              返回新的CallFacade();          }   

   public Note GetCallEntityByCallEntity(Guid CallEntity)
   {
        myCall= null;

       **CallFacade**  callFacade = Get CallFacade();

      if (CallEntity!= Guid.Empty)
       {
           myCall = callFacade.GetCallByEntityId(CallEntity);
       }
       return myCall;
   }
     

CallFacade Not is a Interface

在我的测试中:

[TestInitialize]
    public void TestInitialization()
    {
        IoCMocks.Initialize();
        CommonCallFacadeMock = new CommonCallFacadeMock();
    }
public void GetCallEntityByNoteEntity_GetCall_ReturnValidCall()
    {
        //Arrange
        CallmyNote = GetmyCall(); //private method. == Call.CreateCall();


        //Stubs
        CommonCallFacadeMock.MockCallFacade.Setup(x => x.GetCallByEntityId(ExistingCallEntity)).Returns(myCall);
        //Action
        var obtained = CommonCallFacadeMock.GetCallEntityByCallEntity(ExistingCallEntity);

        CommonCallFacadeMock.MockCallFacade.Verify(x => x.GetCallByEntityId(It.IsAny<Guid>()), Times.Once());


    }

1 个答案:

答案 0 :(得分:0)

您收到此错误消息,因为MOQ只能在抽象类,虚拟方法或具体类的属性上模拟接口,抽象方法或属性。

您必须将方法GetCallByEntityId标记为虚拟。

“Moq Discussions”谷歌小组中有关于虚拟方法的an old discussion