用moq模拟虚拟只读属性 [英] Mocking virtual readonly properties with moq

查看:105
本文介绍了用moq模拟虚拟只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种方法,尽管可以手动完成,为什么不使用最小起订量呢?

I couldn't find a way to do this, though this can be done by hand so why not with moq?

推荐答案

提供此类

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

您可以这样设置Foo(只读属性):

you can set up Foo (a read-only property) like this:

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

stub.Object.Foo现在将返回"bar"而不是"foo".

stub.Object.Foo will now return "bar" instead of "foo".

这篇关于用moq模拟虚拟只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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