繁体   English   中英

如何使用NSubstitute模拟私有setter的属性

[英]how to mock a property with private setter using NSubstitute

我有一个类“示例”,其属性“data”具有私有setter,我想模拟该数据属性

Public class Example { public string data {get; private set;}}

我想使用NSubstitute来模拟数据属性。 有人可以帮我怎么做。

NSubstitute只能在具体类上模拟abstractvirtual方法。 如果您可以修改底层代码以使用接口,那么您可以模拟接口:

public class Example : IExample { public string data { get; private set; } }
public interface IExample { string data { get; } }

[TestMethod]
public void One()
{
    var fakeExample = NSubstitute.Substitute.For<IExample>();
    fakeExample.data.Returns("FooBar");

    Assert.AreEqual("FooBar", fakeExample.data);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM