Mocking virtual readonly properties with moq

一笑奈何 提交于 2019-12-18 13:52:37

问题


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


回答1:


Given this class

public abstract class MyAbstraction
{
    public virtual string Foo
    {
        get { return "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 will now return "bar" instead of "foo".



来源:https://stackoverflow.com/questions/1454186/mocking-virtual-readonly-properties-with-moq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!