简体   繁体   中英

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();   
}

-> Error - Unsupported expression: m => m... The next member after the last one shown above is non-virtual, sealed, or not visible to the proxy factory.

You should make the myflag and detail properties virtual. This allows for the values to be overridden.

public class MyClass 
{
    public virtual bool myflag { get; set; }
    public virtual bool detail { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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