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

问题描述

我找不到这样做的方法,虽然这可以手动完成,那为什么不使用 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 模拟虚拟只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 05:35