访问私有字段 [英] Accessing private fields

查看:79
本文介绍了访问私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,很抱歉,是否已经有人问过这个问题,但是我找不到像我这样的人(但我认为这是一个很常见的问题) 所以我正在尝试做一些单元测试,第一个已经是有问题的.

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..

我必须测试我的类的构造函数,在构造函数中设置一个私有字段的实例.那么如何测试这个PRIVATE字段是否不为null? (因为我假设是我必须测试的东西)->要测试:

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();
    }

->测试方法:

    [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..

推荐答案

在单元测试中,您实际上不必测试类专有的任何内容.内部仅供内部使用的私有成员是该类的实现的一部分,而不是其公开的(和经过测试的)功能的一部分.

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.

这篇关于访问私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆