Moq属性,受保护的二传手 [英] Moq property with protected setter

查看:94
本文介绍了Moq属性,受保护的二传手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下一个对象:

abstract class Foo
{
    public string Bar { get; protected set; }
}

,以便new Mock<Foo>().Bar返回"Blah".

我该怎么做?

fooMock.SetupGet<string>(s => s.Bar).Returns("Blah");

抛出

失败:System.NotSupportedException:在非虚拟成员上的无效设置:s => s.Date

Failure: System.NotSupportedException : Invalid setup on a non-virtual member: s => s.Date

fooMock.Protected().SetupGet<string>("Bar").Returns("Blah");

抛出

要为公共属性StatementSection.Date指定设置,请使用键入的重载

To specify a setup for public property StatementSection.Date, use the typed overloads

推荐答案

由于模拟是通过创建类的代理完成的,因此只有虚拟函数/属性可以被"moqued"

Since mocking is done by creating a proxy of your class,only virtual function/property can be "moqued"

这篇关于Moq属性,受保护的二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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