fkupper / private-sniffer-module
这是一个简单的模块,可以在使用 Codeception 运行单元测试时检测对象中定义的私有元素。
0.1.1
2019-08-20 14:39 UTC
Requires
- php: >=7.1.0
- codeception/codeception: >=2.0.0
This package is auto-updated.
Last update: 2024-09-13 00:27:29 UTC
README
这是一个简单的 Codeception 模块,用于检查对象的私有元素。
安装
可以使用 composer 进行安装
composer require --dev fkupper/private-sniffer-module
用法
只需在您的单元测试配置 YML 文件中包含此模块
modules: enabled: [PrivateSniffer]
在您的单元测试中,您现在可以检查和测试私有属性和方法
class Foo { private $someInt = 1; private sum(int $a, int $b): int { return $a + $b; } } class TestFoo extends \Codeception\Test\Unit { /** * @var \UnitTester */ protected $tester; // tests public function testSum() { $foo = new Foo(); // get the value of the private attribute $someInt $someInt = $this->tester->getPrivatePropertyValue($foo, 'someInt'); $this->assertEquals(1, $someInt); // get a closure of the private method sum $sum = $this->tester->getPrivateMethod($foo, 'sum'); $this->assertEquals(2 + 3, $sum(2, 3)); } }
这是一个测试不良/不可测试代码的解决方案吗?
当然不是。这意味着在代码的关键和敏感部分必须进行测试,而出于任何原因都不可能进行测试时使用。
最佳实践仍然是考虑单元测试来开发,并在一段时间内重构那些无法正确测试的内容。