简体   繁体   English

如何使用 XUnit 验证在没有最小起订量的情况下调用或未调用方法?

[英]How to verify with XUnit that method was called or was not called without Moq?

I have a class that has an update method.我有一个具有更新方法的 class。 In the update method, I'm checking whether its properties are really changed.在更新方法中,我检查它的属性是否真的改变了。

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public void UpdatePerson(Person person)
    {
        if (FirstName == person.FirstName && LastName == person.LastName) return;

        ApplyChanges(person);
    }

    public void ApplyChanges(Person person)
    {
        //....
    }

}

I need to ensure that ApplyChanges method is not called when Person properties are same.我需要确保当Person属性相同时不调用ApplyChanges方法。

This is more a copy-paste, but I believe the recommendation from Microsoft applies to your question:这更像是复制粘贴,但我相信Microsoft 的建议适用于您的问题:

In most cases, there should not be a need to test a private method.在大多数情况下,不需要测试私有方法。 Private methods are an implementation detail.私有方法是一个实现细节。 You can think of it this way: private methods never exist in isolation.你可以这样想:私有方法永远不会孤立存在。 At some point, there is going to be a public facing method that calls the private method as part of its implementation.在某些时候,将有一个面向公众的方法调用私有方法作为其实现的一部分。 What you should care about is the end result of the public method that calls into the private one.您应该关心的是调用私有方法的公共方法的最终结果。

I suggest reading the link I provided in full detail.我建议详细阅读我提供的链接。

Having said that, if you really think this private method needs to be tested, then that implies that it should probably be moved to a separate class .话虽如此,如果你真的认为这个私有方法需要测试,那么这意味着它可能应该被移动到一个单独的 class However, looking at your example, this does not seem to be the case.但是,查看您的示例,情况似乎并非如此。

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

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