简体   繁体   English

如何在没有成对的getter的类上对setter进行单元测试?

[英]How to unit test setters on classes that do not have paired getters?

I'm writing unit tests for classes which have properties that have setters but no getters . 我正在为具有具有setter但没有getter的属性的类编写单元测试。

I want to be able to test these setters to make sure they are setting the data correctly. 我希望能够测试这些设置器 ,以确保它们正确设置了数据。

I find my options are: 我发现我的选择是:

  • write getters for these functions so that I can test if they are set correctly 为这些功能编写吸气剂 ,以便我可以测试它们的设置是否正确
  • write a method such as testAllSetters() which test them all at once 编写诸如testAllSetters()类的方法来一次测试它们

But both solutions are undesirable since it adds unneeded functionality to the class just for the sake of testing it. 但是,这两种解决方案都是不受欢迎的,因为仅出于测试目的,它就向类添加了不必要的功能。

  • I could also test the output of the class to see that it is correct in general, but in many cases this doesn't test the individual setters as I would like 我还可以测试该类的输出,以查看它总体上是正确的,但是在许多情况下,这并不能像我希望的那样测试各个设置器

What is the best way to unit test setters on classes that do not have paired getters? 在没有成对的吸气剂的类上对单元测试设置器进行单元化的最佳方法是什么?

The problem here is that your don't want to change your API for your unit tests. 这里的问题是您不想更改单元测试的API。 Start looking at your unit tests as another user/consumer of your API. 开始以您的API的另一个用户/使用者的身份查看单元测试。 Just like developers using that library, unit tests have their own set of requirements. 就像使用该库的开发人员一样,单元测试也有自己的一套要求。 When you see your unit tests as consumer of your API, there will be a user that uses those getters, and it will justify them. 当您将单元测试视为API的使用者时,将有一个使用这些getter的用户,它将证明它们合理。

When this is not possible to change your API (for instance if you're developing an reusable framework), make the unit testing API internal and use the InternalsVisibleToAttribute to allow your testing library to access internal methods of your code. 如果无法更改您的API(例如,如果您正在开发可重用的框架),则将单元测试API设置为内部,并使用InternalsVisibleToAttribute允许您的测试库访问代码的内部方法。

Leaving unit tests aside, you still might want to consider having getters on those properties, because having properties without getters is very unintuitive for developers. 撇开单元测试,您可能仍要考虑在这些属性上使用吸气剂,因为对于开发人员来说,不使用吸气剂的属性是很不直观的。 The Framework Design Guidelines even have a rule against this: 框架设计准则甚至对此有一条规定:

DO NOT provide set-only properties or properties with the setter having broader accessibility than the getter. 请勿提供仅设置的属性,或者提供的设置程序比获取程序具有更广泛的可访问性的属性。

You might also want to take that into consideration. 您可能还需要考虑到这一点。

Good luck. 祝好运。

您可以在调用设置器后使用PrivateObject来检查私有成员是否已正确更新

Do the setters have any logic? 设置员有逻辑吗?

Yes: Either open up the getter. 是的:要么打开吸气剂。 Java: protected and have the unit test in the same package. Java:受保护并在同一程序包中进行单元测试。 C#: InternalsVisibleToAttribute. C#:InternalsVisibleToAttribute。

No: Don't set the setters directly. 否:请勿直接设置二传手。 No point. 没有意义。 Test the methods that use the data set by the setters. 测试使用设置者数据集的方法。

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

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