简体   繁体   English

我应该访问受保护的单元测试方法吗?

[英]Should I be accessing protected methods for unit tests?

In relation to this question I asked earlier, I am stuck yet again with my unit test. 关于我之前问过的这个问题 ,我的单元测试又被卡住了。

My current problem is in relation to testing the protected methods of my abstract Component class. 我目前的问题是测试我的抽象Component类的protected方法。

I've successfully implemented a mock class, named ConcreteComponent , which does its job very well of inheriting everything the abstract class has. 我已成功实现了一个名为ConcreteComponent的模拟类,它可以很好地继承抽象类所具有的所有功能。

Thing is, I made this concrete class inside my unit test file. 事实是,我在我的单元测试文件中创建了这个具体的类。 The only way of testing protected methods is to have a Private Accessor. 测试受保护方法的唯一方法是使用私有访问器。 However, I cannot create a Private Accessor inside the same file as where the unit test is, and thus, cannot access the protected methods. 但是,我无法在单元测试所在的同一文件中创建私有访问器,因此无法访问受保护的方法。

I've tried to place the mock concrete class in a separate file, under a different namespace, and this now allows me to create a Private Accessor to which the unit test file can now use. 我试图将模拟具体类放在一个单独的文件中,在不同的命名空间下,这现在允许我创建一个单元测试文件现在可以使用的私有访问器。 It worked nicely, but then I figured I need this mock concrete class inside the same file where the unit test is. 它工作得很好,但后来我觉得我需要在单元测试所在的同一文件中使用这个模拟具体类。

So now I have two questions: 所以现在我有两个问题:

1) What are the possible workarounds for this problem? 1)此问题的可能解决方法是什么?

2) Why can't I create a private accessor for a mock class which is inside the same file and namespace as the unit test class? 2)为什么我不能为与单元测试类位于同一文件和命名空间内的模拟类创建私有访问器?

You can take a look at PrivateObject class to gain access to non-public API of your class in your tests. 您可以查看PrivateObject类,以便在测试中访问您的类的非公共API。 It uses reflection internally. 它在内部使用反射。 Protected assets of a class are still api to an external client which in this case is a sub or derived class. 类的受保护资产仍然是外部客户端的api,在这种情况下是外部客户端或子类或派生类。 So desire to test such api is understandable. 所以测试这种api的愿望是可以理解的。 I would not recommend polluting a class to expose public api just for the sake of testing protected api. 我不建议仅为了测试受保护的api而污染一个类来公开public api。 However, since in your case the derived class is in a test project, you can actually provide public api to make testing easier and improve the performance (reflection will be slower and if you are running tests, continuous testing, as you make code changes, it may make test runs slower depending on the number of test etc.). 但是,因为在您的情况下派生类是在测试项目中,您实际上可以提供公共API以使测试更容易并提高性能(反射将更慢,如果您正在运行测试,连续测试,当您进行代码更改时,它可能会使测试运行速度变慢,具体取决于测试数量等)。

Protected functionality is there because you do not want to expose it to your client. 受保护的功能是因为您不希望将其公开给您的客户端。 But If it is protected then may be accessed through the public interface via some satisfying condition if it is not then it is a dead code so remove it. 但如果它受到保护,那么可以通过公共接口通过一些令人满意的条件访问,如果不是那么它是一个死代码,所以删除它。

so the golden rule is 所以黄金法则是

1- Do not try to circumvent testing private / protected methods by using technology (Reflection etc), try to unit test private / protected through the public interface and BTW why your are using VS 2008 Test and why not NUnit instead 1-不要试图通过使用技术(反射等)来规避测试私有/受保护的方法,尝试通过公共接口进行单元测试私有/受保护以及BTW为什么要使用VS 2008测试,为什么不使用NUnit

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

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