2009-09-21 146 views

回答

67

鉴于该类

public abstract class MyAbstraction 
{ 
    public virtual string Foo 
    { 
     get { return "foo"; } 
    } 
} 

您可以设置美孚(只读属性)是这样的:

var stub = new Mock<MyAbstraction>(); 
stub.SetupGet(x => x.Foo).Returns("bar"); 

stub.Object.Foo现在将返回 “巴” 而不是 “富”。

+0

这可以用非抽象类来完成吗? – 2016-03-06 12:33:12

+0

@l - '''''' - ''''''''''''http://stackoverflow.com/a/1973482/126014 – 2016-03-06 12:37:22

+0

优秀答案谢谢。换句话说,最好不要有只有吸气剂的属性,因为它们是嘲弄屁股的痛苦。 – 2016-03-06 12:39:07