访问私有字段
首先,如果问题已经被问过,我很抱歉,但我没有找到像我这样的人(但我认为这是一个非常常见的问题) 所以我试图做一些单元测试,第一个已经有问题了..
我必须测试我的类的构造函数,在构造函数中我设置了一个私有字段的实例..那么我如何测试这个PRIVATE 字段不为空? (因为我认为这是我必须测试的)-->测试:
public BUDGET_MANAGER()
{
this.budget_provider = new BUDGET_PROVIDER();
}
-->测试方法:
[TestMethod()]
public void BUDGET_MANAGERConstructorTest1()
{
BUDGET_MANAGER target = new BUDGET_MANAGER();
Assert.IsNotNull(??,"the provider is not instancied");
}
我怎样才能做到这一点?感谢您的帮助,我在单元测试中迷失了方向..
First I'm sorry if question is already been asked but I don't find anyone like the mine (but I assume it is a pretty common question)
So I'm trying to do some unit tests, and the first one is already problematic..
I have to test the constructor of my class, in the constructor I set an instance of a private field.. So how do I test if this PRIVATE field is not null? (because I assume is that what I have to test)--> To test :
public BUDGET_MANAGER()
{
this.budget_provider = new BUDGET_PROVIDER();
}
--> Test Mehod :
[TestMethod()]
public void BUDGET_MANAGERConstructorTest1()
{
BUDGET_MANAGER target = new BUDGET_MANAGER();
Assert.IsNotNull(??,"the provider is not instancied");
}
How Can I do that? Thanks for help, I'm pretty lost in unit testing..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在单元测试中,您确实不必测试类私有的任何内容。私有的、仅内部已知的成员是类实现的一部分,而不是其公开(和测试)功能的一部分。
基本上,将类的外部可见成员视为其“契约”。这定义了它的实际类型,即其他所有东西所看到的。这就是正在测试的功能。出于很好的原因,内部(私有)成员在类之外是不知道的,不同的类可以使用不同的私有成员以不同的方式实现相同的“契约”(或接口)。
您正在测试的是可见的功能、契约或接口。
In your unit testing you really shouldn't have to test anything that's private to a class. The private, internally-only known members are part of the implementation of the class and not part of its exposed (and tested) functionality.
Basically, think of the externally-visible members of the class as its "contract." That defines its actual type, what everything else sees. And that is the functionality being tested. Internal (private) members aren't known outside of the class for very good reason, a different class can implement that same "contract" (or interface) in a different way, with different private members.
What you're testing is the visible functionality, the contract or interface.
单元测试不应检查私有数据。他们应该测试类的接口定义的行为是否有效,而与任何实现细节无关。
典型的测试将调用构造函数,然后调用公共属性或方法并检查结果是否符合预期。以这种方式测试意味着,如果您稍后将实现更改为(例如)仅在需要时延迟构造 BudgetProvider,那么所有测试仍然有效。实现细节(例如私有成员何时为空或不为空)与您的类的客户端无关,因此无需在单元测试中对其进行测试。
Unit tests should not inspect private data. They should be testing that the behaviour defined by the interface of your class works, independent of any implementation details.
A typical test would call the constructor, and then afterwards call a public property or method and check that the result is as expected. Testing in this way means that if you later change your implementation to (for example) lazily construct a BudgetProvider only when it is needed, then all your tests will still work. Implementation details such as when a private member is or is not null is not relevant to clients of your class and therefore there is no need to test it in your unit tests.
如果您使用 mstest,请右键单击原始类并按创建测试访问器,选择您的测试项目。然后使用访问器测试此条件(应显示在智能感知中)。
但我不确定这是一个好主意,正如其他海报所说。您将使实施变得更难以更改。
If your using mstest, right click the original class and press create test accessor, select your test project. Then test this condition using the accessor (should show up in intellisense).
I'm not sure its a very good idea though, as the other posters have said. You would be making the implementation more difficult to change.
您不应该真正测试类的任何私有变量。
为什么要测试构造函数本身?如果有一些逻辑,测试它是有意义的。例如 - 仅当给定参数正确时才构造对象,并且在创建对象之前进行验证。否则,构造该对象并验证其行为是否符合预期。据推测,如果构造函数工作不正确,对象的行为也会不正确。
还要抵制将私有字段公开为属性的诱惑,只是为了验证它们是否在构造函数中正确设置。
You should't really test any private variables of your classes.
Why would you like to test the constructor itself? It would make sense to test it if there is some logic. For example - you only construct the object if the given parameters are correct and you do validation before you create the object. Otherwise, construct the object and verify that it behaves as expected. Presumably if the constructor works incorrectly the object's behaviour will be incorrect as well.
Also resist the temptation of exposing private fields as properties just to validate that they were set correctly in the constructor.
其他人提到了使用单元测试时不应该做的事情。
我会尝试找到一种方法来完成您想要的操作(您仍然需要测试您的构造函数):
Other guys mentioned what you should not to do when using unit testing.
I'll try to find a way to do what you want (you still need to test your constructor):