单元测试C#保护方法单元测试、方法

2023-09-03 05:44:58 作者:心已死

我来自Java EE的世界,但现在我工作的一个.NET项目。在Java中,当我想测试一个受保护的方法,这是很容易的,只是有测试类相同的包名就足够了。 是否有任何类似的C#?是否有单元测试的保护方法有什么好的做法?我只找到框架和人们说我应该测试唯一的公共方法。 它应该有可能做到这一点而没有任何框架...

I come from the Java EE world but now I'm working on a .Net project. In Java when I wanted to test a protected method it was quite easy, just having the test class with the same package name was enough. Is there anything similar for C#? Is there any good practice for unit testing the protected methods? I only found frameworks and people saying that I should test only public methods. It should be possible to do it without any framework...

多谢了。

推荐答案

您可以继承类,你要测试你的测试类。

You can inherit class you are testing on your test class.

public class Test1 : SomeClass{ Asert.AreEqual(1,SomeClass.ProtectedMethod());}