Moq C# built in class

后端 未结 2 607
野趣味
野趣味 2020-12-20 20:51

I\'m trying to mock out the SearchResultCollection class. However, when I try to intercept a call to the PropertiesLoaded getter, my test throws an Exception:



        
相关标签:
2条回答
  • 2020-12-20 21:05

    This is not possible with Moq. You can only mock interfaces, abstract classes, and classes with virtual methods (and in the latter case, you can only use Setup() to mock the behavior of virtual methods).

    0 讨论(0)
  • 2020-12-20 21:21

    I'm afraid you can't.

    Like you said the property isn't virtual. Another option would have been to mock the interface but I checked and there isn't one for this class (according to the MSDN doc).

    There are some other isolation framework which can do this though. Microsoft Moles is able to do it, so is TypeMock.


    Microsoft Moles: http://research.microsoft.com/en-us/projects/moles/

    TypeMock : http://www.typemock.com/

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