起订量C#建类 [英] Moq C# built in class

查看:168
本文介绍了起订量C#建类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嘲弄了SearchResultCollection类。然而,当我试图拦截对PropertiesLoaded吸气打电话,我的测试抛出一个异常:

  System.NotSupportedException:无效的设置一个非虚拟(VB中重写)成员:X => x.PropertiesLoaded 



我的代码:

 模拟< SearchResultCollection> sea​​rchResultMock =新的模拟< SearchResultCollection>(); 

//设置集合将返回
的String [] = TempData的{一,二};
searchResultMock.SetupGet(X => x.PropertiesLoaded).Returns(TempData的);



有没有人成功嘲笑像这样一类?在有关财产只有一个getter,而不是虚拟的。



  // 
//摘要:
//获取是在执行搜索前
//指定的System.DirectoryServices.DirectorySearcher属性。
//
//返回:
// System.String类型的数组,它包含已在System.DirectoryServices.DirectorySearcher.PropertiesToLoad属性指定
//属性
//集合在执行搜索前。
公共字符串[] {PropertiesLoaded获得; }


解决方案

恐怕你不能。



就像你说的财产不虚。另一种选择会是向模拟接口,但我检查,没有一个此类(根据在MSDN DOC)。



有一些其他的隔离框架,虽然可以做到这一点。
微软痣是能够做到这一点,所以TypeMock






微软痣:的http://research.microsoft.com/en-us/projects/moles/



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


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:

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: x => x.PropertiesLoaded

My Code:

Mock<SearchResultCollection> searchResultMock = new Mock<SearchResultCollection>();

// Set up collection that will be returned
string[] tempData = { "one", "two" };
searchResultMock.SetupGet(x => x.PropertiesLoaded).Returns(tempData);

Has anyone successfully mocked out a class like this? The property in question only has a getter and is not virtual.

    //
    // Summary:
    //     Gets the System.DirectoryServices.DirectorySearcher properties that were
    //     specified before the search was executed.
    //
    // Returns:
    //     An array of type System.String that contains the properties that were specified
    //     in the System.DirectoryServices.DirectorySearcher.PropertiesToLoad property
    //     collection before the search was executed.
    public string[] PropertiesLoaded { get; }

解决方案

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/

这篇关于起订量C#建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆