PHPUnit 在带有命名空间的 Netbeans 中失败
我有:PHP 5.3.8、PHPUnit 3.5.15、Netbeans 7.0.1
在使用 Netbeans 的标准示例进行 PHPUnit 测试时,它运行得很好。 通过仅添加“命名空间测试;”我收到错误消息 Calculator.php 不是文件也不是目录。如何解决这个问题呢? (我想在我的项目中使用命名空间声明)
要测试的类:
namespace test;
class Calculator {
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
* @assert (1, 2) == 4
*/
public function add($a, $b) {
return $a + $b;
}
}
?>
单元测试:
require_once dirname(__FILE__) . '/../Calculator.php';
/**
* Test class for Calculator.
* Generated by PHPUnit on 2011-09-11 at 00:52:24.
*/
class CalculatorTest extends PHPUnit_Framework_TestCase {
/**
* @var Calculator
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->object = new Calculator;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* Generated from @assert (0, 0) == 0.
*/
public function testAdd() {
$this->assertEquals(
0, $this->object->add(0, 0)
);
}
/**
* Generated from @assert (0, 1) == 1.
*/
public function testAdd2() {
$this->assertEquals(
1, $this->object->add(0, 1)
);
}
/**
* Generated from @assert (1, 0) == 1.
*/
public function testAdd3() {
$this->assertEquals(
1, $this->object->add(1, 0)
);
}
/**
* Generated from @assert (1, 1) == 2.
*/
public function testAdd4() {
$this->assertEquals(
2, $this->object->add(1, 1)
);
}
/**
* Generated from @assert (1, 2) == 4.
*/
public function testAdd5() {
$this->assertEquals(
4, $this->object->add(1, 2)
);
}
}
?>
I have: PHP 5.3.8, PHPUnit 3.5.15, Netbeans 7.0.1
While using the standard example of Netbeans for PHPUnit testing, it runs perfectly.
By adding just the "namespace test;" I get the error that Calculator.php is not a file nor a directory. How to solve this problem? (I would like to use the namespace declarative in my project)
THE CLASS TO TEST:
namespace test;
class Calculator {
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
* @assert (1, 2) == 4
*/
public function add($a, $b) {
return $a + $b;
}
}
?>
THE UNIT TEST:
require_once dirname(__FILE__) . '/../Calculator.php';
/**
* Test class for Calculator.
* Generated by PHPUnit on 2011-09-11 at 00:52:24.
*/
class CalculatorTest extends PHPUnit_Framework_TestCase {
/**
* @var Calculator
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->object = new Calculator;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* Generated from @assert (0, 0) == 0.
*/
public function testAdd() {
$this->assertEquals(
0, $this->object->add(0, 0)
);
}
/**
* Generated from @assert (0, 1) == 1.
*/
public function testAdd2() {
$this->assertEquals(
1, $this->object->add(0, 1)
);
}
/**
* Generated from @assert (1, 0) == 1.
*/
public function testAdd3() {
$this->assertEquals(
1, $this->object->add(1, 0)
);
}
/**
* Generated from @assert (1, 1) == 2.
*/
public function testAdd4() {
$this->assertEquals(
2, $this->object->add(1, 1)
);
}
/**
* Generated from @assert (1, 2) == 4.
*/
public function testAdd5() {
$this->assertEquals(
4, $this->object->add(1, 2)
);
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Tomasz 的答案当然是正确的。作为一个小插件:
据我了解,将测试放在与生产类相同的命名空间中已成为常见做法。
然后你可以继续使用
@Tomasz answer is, of course, correct. As a little addon:
From what i understand it has become common practice to put your tests in the same namespace as your production class.
then you can continue using
了解如何在代码中使用命名空间类。您需要创建计算器类实例,而不是:
但是:
我假设该类已加载。如果没有看到您的自动加载器或正确的文件路径。
Learn about using namespaced classes in your code. You need to create Calculator class instance not with:
but:
I assume that class is loaded. If not see your autoloader or correct file path.