PHPUnit:对非公共变量进行断言 [英] PHPUnit: Doing assertions on non-public variables

查看:70
本文介绍了PHPUnit:对非公共变量进行断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有私有财产以及相关的公共获取器和设置器的类.我想用PHPUnit测试该属性在使用setter之后是否获得正确的值,或者该getter返回正确的属性.

Suppose I have a class with a private property and associated public getter and setter. I want to test with PHPUnit that the property gets the correct value after the setter has been used or that the getter returns the correct property.

当然,我可以通过使用getter来检查对象是否存储了正确的值,从而对setter进行测试,反之亦然.但是,这不能保证私有财产就是正在设置的私有财产.

Of course I can test the setter by using the getter to see that the object is storing the correct value, and vice versa for testing the getter. However, this doesn't guarantee that the private property is the one being set.

说我上了下面的课.我创建了一个属性,getter和setter.但是我在属性名称中打了一个错字,所以getter和setter实际上并没有操纵它们要操纵的属性

Say I had the following class. I created a property, getter and setter. But I made a typo in the property name, so the getter and the setter don't actually manipulate the property they're meant to manipulate

class SomeClass
{
    private 
        $mane = NULL; // Was supposed to be $name but got fat-fingered!

    public function getName ()
    {
        return ($this -> name);
    }

    public function setName ($newName)
    {
        $this -> name = $newName;
        return ($this);
    }
}

如果我运行以下测试

public function testSetName ()
{
    $this -> object -> setName ('Gerald');
    $this -> assertTrue ($this -> object -> getName () == 'Gerald');
}

我会通过的.但是,实际上发生了我不想要的非常糟糕的事情.调用setName()时,它实际上在该类中创建了一个新属性,其名称我认为我的私有属性具有该属性,只有setter创建的属性才是公共的!我可以使用以下代码进行演示:

I would get a pass. However, something very bad has actually happened that I don't want. When setName() is called, it actually creates a new property in the class with the name I thought my private property had, only the one that the setter creates is public! I can demonstrate that with the following code:

$a  = new SomeClass;

$a -> setName('gerald');
var_dump ($a -> getName ());
var_dump ($a -> name);

它将输出:

string(6)杰拉德"

string(6) "gerald"

string(6)杰拉德"

string(6) "gerald"

有什么办法可以从PHPUnit访问私有属性,以便编写测试以确保我认为正在获取和设置的属性实际上在真正正在获取和设置?

Is there any way I can access the private properties from PHPUnit so I can write tests that make sure that the properties I think are being get and set actually really are being get and set?

或者我应该在测试中做其他事情来发现这样的问题,而不尝试访问被测对象的私有状态吗?

Or is there some other thing I should be doing in a test to catch problems like this without trying to get access to the private state of the object under test?

推荐答案

对于测试属性,我将使用与讨论私有方法测试时相同的参数.

For testing properties, I'd make the same arguments I make then talking about testing private methods.

You usually don't want to do this .

这是关于测试可观察到的行为.

It's about testing observable behavior.

如果重命名所有属性或决定将它们存储到数组中,则根本不需要调整测试.您希望测试告诉您一切仍然有效!当您需要更改以确保一切正常时,您将失去所有好处,因为更改测试也可能会出错.

If you rename all your properties or decide to store them into an array you should not need to adapt your tests at all. You want your tests to tell you that everything still works! When you need to change the tests to make sure everything still works you lose all the benefits as you also could make an error changing the tests.

因此,总而言之,您会失去测试套件的价值!

So, all in all, you lose the value of you test suite!

仅测试get/set组合就可以了,但是通常不是每个setter都应该有一个getter,仅创建它们进行测试并不是一件好事.

Just testing the get/set combinations would be ok enough but usually not every setter should have a getter and just creating them for testing is not a nice thing ether.

通常,您先设置一些东西,然后告诉该方法DO(行为).对此进行测试(由类完成应做的事情)是进行测试的最佳选择,并且应该使测试属性变得多余.

Usually, you set some stuff and then tell the method to DO (behavior) something. Testing for that (that the class does what is should do) is the best option for testing and should make testing the properties superfluous.

如果您真的想这样做,则在PHP Reflections API中提供setAccessible功能,但是我无法组成一个我认为很理想的示例

If you really want to do that there is the setAccessible functionality in PHP reflections API but I can't make up an example where I find this desirable

PHP混乱检测器作为这将为您生成两个警告,因为从未访问过变量

This will generate two warnings for you because the variables are never accessed

这篇关于PHPUnit:对非公共变量进行断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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