gpt4 book ai didi

c# - 为什么不推荐使用私有(private)访问器?

转载 作者:太空狗 更新时间:2023-10-30 01:03:59 25 4
gpt4 key购买 nike

过去,我们选择 MSTest 的首要原因是我们可以访问和测试私有(private)方法。现在Private accessors are deprecated in Visual Studio 2012 .有谁知道微软为什么做出这样的决定?是因为测试私有(private)方法不是一个好习惯吗?

此外,如果我仍然需要对私有(private)方法进行单元测试,我该如何在 VS 2012 及更高版本中进行测试?

最佳答案

根据VS团队文章Generation of Private Accessors...由于以下原因,此功能已于 2010 年弃用:

  1. Lack of resources and time: The focus for this release has been to improve the experience for manual testers, so the priority for the code generation and publicize features has been lowered. There have also been other issues with the publicize functionality that we utilize that have not been addressed.

  2. New features by Language teams: As the language teams have made modifications to their project types and languages, we have been unable to respond to the changes they have made and have not been able to work with the new features they have introduced.

(更多历史笔记可通过以下搜索找到:mstest why private accessors depricated site:blogs.msdn.com)

对于问题的第二部分 - 通常您不需要对私有(private)方法进行单元测试。如果您真的觉得需要公开此类测试方法 - 请考虑标记 internal 并使用“friend”(InternalsVisibleToAttribute)是否适合您的情况。

关于c# - 为什么不推荐使用私有(private)访问器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com