最小起订量 - 如何验证属性值是通过设定器设置最小、属性、起订量

2023-09-03 04:42:12 作者:习惯ノ寂寞

考虑这个类:

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

   public virtual void CheckIn()
   {
      //Do Nothing for now as demonstrating false positive test.
   }
}

签入的方法是故意空。现在我有一些测试方法,以验证调用每个方法的状态。

The Checkin method is intentionally empty. Now i have a few test methods to verify the status of calling each method.

[TestMethod]
public void CheckOutSetsCheckedOutStatusToTrue()
{
    Content c = new Content();    
    c.CheckOut();
    Assert.AreEqual(true, c.IsCheckedOut); //Test works as expected
}

[TestMethod]
public void CheckInSetsCheckedOutStatusToFalse()
{
    Content c = new Content();
    c.CheckIn();
    Assert.AreEqual(false, c.IsCheckedOut); //Test does not work as expected
}

第二测试通过错误的原因。所以,我怎么可以用嘲讽(MOQ)来验证检入被设置IsCheckedOut属性?

The 2nd test passes for the wrong reasons. So how can i use mocking (moq) to verify that CheckIn is setting the IsCheckedOut property?

感谢。

修改

要澄清:我有()呼吁检入的方法,他们的工作是把IsCheckedOut状态设置为false。

To clarify: I have a method called CheckIn() whose job it is to set the IsCheckedOut status to false.

您将在我的测试code以上,该测试将返回false,即使我没有设置该属性值设置为false看;这是预料之中,没有什么错在这里。

You will see in my test code above that the Test will return false even if i do not set the property value to false; This is expected, nothing wrong here.

我觉得我的问题具体是如何确认的检入()方法的IsCheckedOut属性设置为false?这就是我所说的行为验证。

I think my question specifically is How can i verify that the CheckIn() method has set the IsCheckedOut property to false? This is what I would call behavioral verification.

我相信一些评论建议做一些这相当于国家核查?如果是这样,我不相信这是在所有的嘲笑这部分的任何值时,我们可以简单地使用:

I believe some of the comments suggested doing something which amounts to state verification? If so I don't believe there is any value in mocking this part at all when we can simply use:

Content c = new Content();    
c.CheckIn();    
Assert.AreEqual(false, c.IsCheckedOut); //State verification

当然,我可能是错的,所以请大家帮我澄清一下这些概念:)

Of course I may be wrong, so please help me clarify these concepts :)

推荐答案

下面应该工作。配置您的模拟对象为:

The following should work. Configure your mock object as:

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

和测试后code:

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

我没有测试过,无论如何,所以请告诉我,如果你的作品。

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

修改。事实上,这不会因为制定者的工作 IsCheckedOut 是假的。

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

不管怎样,现在我明白了,你从来没有设置 IsCheckedOut 的一流的施工时间的价值。这将是一个好主意,添加以下到内容类:

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