Moq - How to verify that a property value is set via the setter

前端 未结 5 1886
梦如初夏
梦如初夏 2020-12-16 09:22

Consider this class:

public class Content
{      
   public virtual bool IsCheckedOut {get; private set;}
   public virtual void CheckOut()
   {
      IsChec         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 09:22

    The following should work. Configure your mock object as:

    var mock=new Mock();
    mock.SetupSet(content => content.IsCheckedOut=It.IsAny()).Verifiable();
    

    And after the test code:

    mock.VerifySet(content => content.IsCheckedOut=It.IsAny());
    

    I haven't tested it anyway, so please tell me if it works for you.

    EDIT. Indeed, this will not work since the setter for IsCheckedOut is false.

    Anyway, now I see that you never set the value of IsCheckedOut at class construction time. It would be a good idea to add the following to the Content class:

    public Content()
    {
        IsCheckedOut=false;
    }
    

提交回复
热议问题