运行PHPUnit测试时收到以下错误:
1)警告 为CarTest :: countWheels指定的数据提供程序无效。 语法错误,意外'浮动' (T_STRING),期待变量(T_VARIABLE)
为什么我不能使用成员变量的Wheel类并在CarTest类中引用Wheel :: NUMBER_OF_WHEELS?
这是因为在单元测试中,你应该只测试类本身的功能吗?在我的情况下,只测试Car类中的项目而不引用外部类,例如Wheel?
但是,如果我要在类Wheel中注释掉成员变量:
// private float tirePressure = null;
我没有错误。注意,我没有注释掉成员函数。所以,成员函数不会导致问题...但是,成员变量......它不喜欢?
或者,如果我要删除Wheel类,而是执行以下操作:
1)在Car类中,定义
const NUMBER_OF_WHEELS = 4;
2)在CarTest课程中
更改自:
'Good Data: (alpha)' => [2, Wheel::NUMBER_OF_WHEELS, 8]
更改为:
'Good Data: (alpha)' => [2, Car::NUMBER_OF_WHEELS, 8]
我没有错误。 AND,我在Car类中定义了成员变量。
我生成错误的代码
// =============== Car.php
<?php
declare(strict_types=1); // strict typing
namespace App;
class Car
{
private $make;
private $model;
private $color;
function __construct(string $make)
{
$this->make = $make;
echo "OK\n";
}
public function getMake() : string
{
return $this->make;
}
public function countWheels(int $numCars, int $numWheels) : int
{
return $numCars * $numWheels;
}
}
// =============== Wheel.php
<?php
namespace App;
class Wheel
{
/**
* @var int Typical number of wheels for a car.
*/
const NUMBER_OF_WHEELS = 4;
private float tirePressure = null;
public function sayHelloWorld()
{
echo "Hello World!\n";
}
}
// =============== CarTest.php
<?php
declare(strict_types=1); // strict typing
use PHPUnit\Framework\TestCase;
use App\Car;
use App\Wheel;
class CarTest extends TestCase
{
/**
* ========================================================================
* @test
* ========================================================================
*/
public function true_asserts_to_true()
{
$this->assertTrue(true);
}
/**
* ========================================================================
* Test: Get make of Car
*
* @dataProvider getMakeProvider
* @test
* ========================================================================
*/
public function getMake($expected)
{
$myCar = new Car("Ford");
$actual = $myCar->getMake();
$this->assertEquals($expected, $actual);
}
/**
* ========================================================================
* Data Provider for: getMake
* ========================================================================
*/
public function getMakeProvider()
{
// NOTE: This is an associative array. Therefore, each index must be unique (hence, 'alpha', 'beta', etc.).
return [
// NOTE: To generate a failure, set the expected value to something other than the actual value.
'Good Data: (alpha)' => ["Ford"]
];
}
/**
* ========================================================================
* Test: Get make of Car
*
* @dataProvider countWheelsProvider
* @test
* ========================================================================
*/
public function countWheels($numCars, $numWheels, $expected)
{
// echo "PHPUnit: " . PHPUnit_Runner_Version.id() ."\n";
$myCar = new Car("Ford");
$actual = $myCar->countWheels($numCars, $numWheels);
$this->assertEquals($expected, $actual);
}
/**
* ========================================================================
* Data Provider for: countWheels
* ========================================================================
*/
public function countWheelsProvider()
{
// NOTE: This is an associative array. Therefore, each index must be unique (hence, 'alpha', 'beta', etc.).
return [
// NOTE: To generate a failure, set the expected value to something other than the actual value.
'Good Data: (alpha)' => [2, Wheel::NUMBER_OF_WHEELS, 8]
];
}
}
我的系统:
Sebastian Bergmann和贡献者的PHPUnit 6.3.0。 运行时:PHP 7.0.24-1 + ubuntu14.04.1 + deb.sury.org + 1与Xdebug 2.5.5
谢谢
答案 0 :(得分:1)
在PHP中,你不能给类变量一个类型......
private float tirePressure = null;
需要
private $tirePressure = null;
此外,您需要让$
表示变量。