C# 存根基类中的只读属性

C# 存根基类中的只读属性,c#,unit-testing,microsoft-fakes,C#,Unit Testing,Microsoft Fakes,我有一门课是这样的: public class Customer : CustomerBase { // internals are visible to test internal string GenString() { // this actually composes a number of different properties // from the parent, child and system properties

我有一门课是这样的:

public class Customer : CustomerBase
{
    // internals are visible to test
    internal string GenString()
    {
        // this actually composes a number of different properties 
        // from the parent, child and system properties
        return this.InfoProperty.Name + DateTime.Now.ToString() + "something else"; 
    }
}

// this class is in a 3rd party library, but from metadata it looks like this
public class CustomerBase
{
    public Info InfoProperty { get; }
}
public class Tests
{
    public void MyTest()
    {
        using (ShimsContext.Create())
        {
            // Arrange
            /* I shim datetime etc. static calls */

            Fakes.StubCustomer c = new Fakes.StubCustomer()
            {
                InfoProperty = new Info("Name") // <- Error here because it's readonly
            };

            // Act
            string result = c.GenString();

            // Assert
            Assert.AreEqual(result, "whatnot");
        }
    }
}
我的测试如下所示:

public class Customer : CustomerBase
{
    // internals are visible to test
    internal string GenString()
    {
        // this actually composes a number of different properties 
        // from the parent, child and system properties
        return this.InfoProperty.Name + DateTime.Now.ToString() + "something else"; 
    }
}

// this class is in a 3rd party library, but from metadata it looks like this
public class CustomerBase
{
    public Info InfoProperty { get; }
}
public class Tests
{
    public void MyTest()
    {
        using (ShimsContext.Create())
        {
            // Arrange
            /* I shim datetime etc. static calls */

            Fakes.StubCustomer c = new Fakes.StubCustomer()
            {
                InfoProperty = new Info("Name") // <- Error here because it's readonly
            };

            // Act
            string result = c.GenString();

            // Assert
            Assert.AreEqual(result, "whatnot");
        }
    }
}
公共类测试
{
公共无效MyTest()
{
使用(ShimsContext.Create())
{
//安排
/*我可以拨打datetime等静态电话*/
Fakes.StubCustomer c=新的Fakes.StubCustomer()
{

InfoProperty=new Info(“Name”)//如何将此getter包装到可由模拟覆盖的临时虚拟方法中

例如:

公共类客户:CustomerBase
{
//内部构件对试验可见
内部字符串GenString()
{
//这实际上由许多不同的属性组成
//从父级、子级和系统属性
返回InfoPropertyNameGetter()+DateTime.Now.ToString()+“其他内容”;
}
公共虚拟字符串InfoPropertyNameGetter(){
重新注册this.InfoProperty.Name;
}
}
Mock Mock=新Mock();
Setup(m=>m.InfoPropertyNameGetter())。返回(“我的自定义值”);

它看起来有点像

一些想法中描述的
引入实例Delegator
模式-我会重新考虑将私有成员声明为内部成员,以使单元测试更容易,如果这里是实际情况的话。您可以使用反射来实现它。其次,为什么要存根它?我的意思是,如果您已经是f使用
DateTime
&
InfoProperty
,为什么不让它连接起来呢?您可以使用反射绕过访问修饰符(
private
),但这是不可取的。该属性由于某些原因(可能是派生值)是只读的。您应该找到间接设置它的方法(在构造函数中?通过2或3个其他方法?)@KeithPayne:我想测试的是合法的内部方法,所以这不成问题。问题是存根InfoProperty(如示例所示)不起作用。