![](/img/trans.png)
[英]How can I mock or set up values for a unit test that are normally set to a static property in startup?
[英]How to set default value inside mock unit test for class property?
public MyClass
{
bool myflag ;
bool detail ;
}
private async Task DemoAsync()
{
MyClass myclassobj = new();
}
//But in test case I want to set default value for `myflag` and `detail` as `true` using mock I'm trying like below but getting error.
[TestMethod()]
public async Task Demotest()
{
bool someProperty = true;
var mock = new Mock<MyClass>();
mock.SetupSet(m => m.myflag = It.IsAny<bool>())
.Callback<bool>(p => someProperty = p)
.Verifiable();
}
-> 錯誤 - 不支持的表達式:m => m... 上面顯示的最后一個成員之后的下一個成員是非虛擬的、密封的或對代理工廠不可見。
您應該將myflag和detail屬性設為虛擬。 這允許覆蓋值。
public class MyClass
{
public virtual bool myflag { get; set; }
public virtual bool detail { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.