Moq C#内置

前端之家收集整理的这篇文章主要介绍了Moq C#内置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试模拟SearchResultCollection类.但是,当我尝试拦截对PropertiesLoaded getter的调用时,我的测试会抛出异常:
  1. System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: x => x.PropertiesLoaded

我的代码

  1. Mock<SearchResultCollection> searchResultMock = new Mock<SearchResultCollection>();
  2.  
  3. // Set up collection that will be returned
  4. string[] tempData = { "one","two" };
  5. searchResultMock.SetupGet(x => x.PropertiesLoaded).Returns(tempData);

有没有人成功嘲笑过这样的课程?有问题的财产只有一个吸气剂,而不是虚拟的.

  1. //
  2. // Summary:
  3. // Gets the System.DirectoryServices.DirectorySearcher properties that were
  4. // specified before the search was executed.
  5. //
  6. // Returns:
  7. // An array of type System.String that contains the properties that were specified
  8. // in the System.DirectoryServices.DirectorySearcher.PropertiesToLoad property
  9. // collection before the search was executed.
  10. public string[] PropertiesLoaded { get; }

解决方法

我怕你不能.

就像你说的那样,财产不是虚拟的.另一个选择是模拟界面,但我检查了这个类没有一个(根据MSDN文档).

还有一些其他隔离框架可以做到这一点.
Microsoft Moles能够做到这一点,TypeMock也是如此.

微软摩尔:http://research.microsoft.com/en-us/projects/moles/

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

猜你在找的C#相关文章