简体   繁体   English

C# 中的单元测试链式私有方法

[英]Unit testing chained private methods in C#

My problem is that I have public method in my class and it is calling a private method.我的问题是我的 class 中有公共方法,它正在调用私有方法。 The private method is calling another private method and so on with 4 chained private methods.私有方法正在调用另一个私有方法,依此类推,有 4 个链式私有方法。 I know that I should write unit tests only for public methods.我知道我应该只为公共方法编写单元测试。 In my case I will have full code coverage, because all private methods are called from the public method.就我而言,我将拥有完整的代码覆盖率,因为所有私有方法都是从公共方法调用的。 But in case something goes wrong my unit test won't know exactly which method screwed it up.但万一出现问题,我的单元测试将不知道究竟是哪种方法搞砸了。 I know that I should try to move some of my methods in separate classes, so that I can test them but this means that I should make 4 different classes with only one method in each.我知道我应该尝试将我的一些方法移动到单独的类中,以便我可以测试它们,但这意味着我应该创建 4 个不同的类,每个类中只有一个方法。

So is there a way to test each of these private methods, or I just should use the integrated feature in Visual Studio for testing private methods?那么有没有办法测试这些私有方法中的每一个,或者我应该使用 Visual Studio 中的集成功能来测试私有方法?

You are already testing the private methods by calling the public one.已经通过调用公共方法来测试私有方法。 You test the input and output of the public method - which relies on the private ones, hence all are being tested.您测试公共方法的输入和 output - 它依赖于私有方法,因此所有方法都在测试中。 Any problems that occur will result in an exception which (upon checking the stack trace) will inform you which method (private or otherwise) is causing the problem.发生的任何问题都将导致异常(在检查堆栈跟踪时)将通知您哪个方法(私有或其他)导致问题。

Private members are just that, private .私人成员就是这样,私人的。 They shouldn't be exposed to the outside since they're only concerned with the inner workings of the object, hence unit tests should (and can only) test the public ones, which is what you're already doing.它们不应该暴露在外部,因为它们只关心 object 的内部工作,因此单元测试应该(并且只能)测试公共测试,这就是你已经在做的。

You should concentrate on testing observable behaviour, which means testing expected consequences of calling public methods.您应该专注于测试可观察的行为,这意味着测试调用公共方法的预期结果。 If the private methods are performing a lot of work then consider moving them into objects injected into the object under test, if not then if something goes wrong it shouldn't be too hard to debug:)如果私有方法正在执行大量工作,则考虑将它们移动到注入到正在测试的 object 的对象中,如果没有,那么如果出现问题,调试应该不会太难:)

Stoimen,斯托门,

I agree with you.我同意你的看法。

I read many of forums saying that PRIVET methods should not being tested because it is within other public tests.我读过很多论坛都说 PRIVET 方法不应该被测试,因为它在其他公共测试中。 But the main reason is to isolate and make it simple to build blocks and create isolated levels of test.但主要原因是隔离并使构建块和创建隔离级别的测试变得简单。 Not for you but for others that do not agree with my opinion, here is one simple example: Public method BuildCar (sizeOfChassi, typeOfBody, Color) { Call private MakeChassi (sizeOfChassi) Call private MakeBody (typeOfBody) Call private Paint (Color) } In this scenario, I don't want to be public MakeChassi, MakeBody and Paint because I just build and sell cars, but I want to thoroughly test my factory.不是为你,而是为其他不同意我意见的人,这里有一个简单的例子:公共方法 BuildCar (sizeOfChassi, typeOfBody, Color) { Call private MakeChassi (sizeOfChassi) Call private MakeBody (typeOfBody) Call private Paint (Color) }在这种情况下,我不想公开 MakeChassi、MakeBody 和 Paint,因为我只是制造和销售汽车,但我想彻底测试我的工厂。 If I just use the public method, I'll need to create a lot of testing variations and of course I have a risk to forget some.如果我只使用 public 方法,我将需要创建很多测试变体,当然我有忘记一些的风险。 Just testing the small parts (the main reason of UNIT TESTs), I'm pretty sure that they are working.只是测试小部件(单元测试的主要原因),我很确定它们正在工作。 Other thing is inside the class I could create another public method to use those privates, without need to re-write tests variants.另一件事是在 class 内部,我可以创建另一个公共方法来使用这些私有,而无需重新编写测试变体。

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

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