测试使用 NUnit 调用私有方法的公共方法 [英] Test a public method which calls a private method using NUnit

查看:65
本文介绍了测试使用 NUnit 调用私有方法的公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有一个公共方法,它在内部调用该类中的特定私有方法.它看起来像这样:

I have a public method in a class that internally calls a particular private method within that class. It looks something like this :

public class MyClass : IMyClassInterface
{
    public List<int> MyMethod(int a, int b)
    {
        MyPrivateMethod(a, b, ref varList, ref someVal);
    }
    private void MyPrivateMethod(int a, int b, ref List<int> varList, ref double someval)
    {
    }
}

现在,我基本上想使用 NUnit 来测试这个公共方法.我正在使用 NMock 2.0 进行模拟.我该怎么做?因为,它在内部调用了这个我不想公开的私有方法.或者如果我将私有方法改为受保护,有没有办法做到这一点?

Now, I basically want to test this public method using NUnit. I am using NMock 2.0 for mocking. How do I do it? Since, it internally calls this private method which I do not want to make public. Or is there a way to do it if I turn the private method to protected instead?

推荐答案

现在,我基本上想测试这个公共方法(...)

Now, I basically want to test this public method (...)

这太棒了.这是你应该做的.暂时忘记内部细节.从公共方法的角度来看,这两个片段之间有什么区别吗?

This is great. This is what you should be doing. Forget about internal details for a moment. From public method point of view, is there any difference between these two snippets?

// Your current implementation
public void MyMethod(int a, int b)
{
    MyPrivateMethod(a, b);
}
private void MyPrivateMethod(int a, int b)
{
    var c = a + b;
    // some more code
}

// Private method inlined
public void MyMethod(int a, int b)
{
    var c = a + b;
    // some more code
}

调用(public)MyMethod 的人不会注意到这两者之间的任何区别.最终结果是一样的.是否存在私有方法调用并不重要,因为就公共 API 而言,它是无关紧要的.您可以内联所述私有方法,使其永远消失,并且从公共消费者的角度来看没有任何变化.最终结果是唯一重要的事情.您测试代码使用者可观察到的最终结果.不是一些内部的胡言乱语.

Whoever calls (public) MyMethod will not be able to notice any difference between these two. End result is the same. It doesn't matter there is a private method call, because as far a public API is concerned it is irrelevant. You could inline said private method, make it gone forever, and from public consumer point of view nothing changes. End result is the only thing that's important. You test end result observable by code consumer. Not some internal gibberish.

重要的认识是:

正确设计的 SOLID 代码永远不会让您处于需要您进行私人模拟的位置.问题的根源?糟糕的设计.

Properly designed SOLID code will never put you in a position which will require you to do private mocking. Source of the problem? Bad design.

来源:如何模拟私有方法 - 解决方案

是的.悲伤但真实,你的设计不是那么好.根据您是否想改变这一点,您可以采取以下几种方法:

Yep. Sad but true, your design is not that great. Depending on whether you want to change that or not, there are few approaches you could take:

  • 不要试图模仿私人细节,专注于公共 API(无助于设计问题)
  • 将私有方法提取到类中,引入依赖项(长期解决方案,改进设计并使代码易于测试)
  • 保护私有方法,按照其他答案中的建议在测试中覆盖(对设计问题没有帮助,可能不会产生有价值的测试)

无论你选择哪个,我都留给你.但是,我会再强调一次 - 模拟私有方法不是单元测试、库或工具问题 - 它是一个设计问题,因此最好解决.

Whichever you chose I leave up to you. However, I'll emphasize it one more time - mocking private method is not unit testing, library or tools problem - it is a design problem and is best solvable as such.

附带说明,(如果可以)不要使用 NMock2.这是一个从 2009 年开始进行最新更改的图书馆.就像拥有一辆 30 岁的汽车,但最后一次维修是在 15 年前.现在有更好的(FakeItEasy、Moq、NSubstitute).

On a side note, (if you can) don't use NMock2. It's a library with last changes from 2009. It's like having a 30 year old car which was last serviced 15 years ago. There are much better ones nowadays (FakeItEasy, Moq, NSubstitute).

这篇关于测试使用 NUnit 调用私有方法的公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆