如何测试使用抽象类的插件接口?
我正在使用 PHP 5.3 和 SimpleTest,但欢迎更一般的答案。每个插件都是一个扩展抽象类的类...我如何测试这个接口是否正常工作?我是否必须创建多个插件并测试它们?或者有更令人满意的方法吗?
举个例子,想象一下写一些东西来代表金钱。用户可以使用不同的货币类别来扩展此功能。
抽象类Money
{
私有静态$符号;
私有静态$num_decimals;公共函数 __construct($amount) { ...}
公共函数 __toString() { ... }
}
插件将如下所示:
GBPound 类扩展了 Money
{
私有静态 $symbol = "£";
私有静态$num_decimals = 2;
}
我可以模拟多重继承,扩展 UnitTest 类和 Money 类,但这可能会变得混乱!
这很困难,因为单元测试是关于测试接口的,但类本身是插件接口。
I'm using PHP 5.3 and SimpleTest, but more general answers are welcome. Each plug-in will be a class which extends an abstract class... how do I test that this interface works properly? Do I have to create several plug-ins and test them? Or is there a more satisfying way?
As an example, imagine writing something to represent money. Users can extend this with different currency classes.
abstract class Money
{
private static $symbol;
private static $num_decimals;public function __construct($amount) { ...}
public function __toString() { ... }
}
Then a plugin would look like this:
class GBPound extends Money
{
private static $symbol = "£";
private static $num_decimals = 2;
}
Perhaps I could emulate multiple inheritance, extending the UnitTest class and the money class, but that could get messy!
It's so difficult because unit testing is about testing the interface, but the class itself is the plug-in interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定 simpletest,但 PHPUnit 可以创建抽象类的模型,允许您直接测试它们。
Not sure about simpletest, but PHPUnit can create mockups of abstract classes, that allow you to test them directly.
我决定采用通过创建一个空插件类来测试接口的想法。对我来说,这似乎比模拟抽象类干净得多。 (但是,出于其他原因,我正在将所有测试迁移到 PHPUnit。)
I decided to go with the idea of testing the interface by creating an empty plug-in class. To me this seems much cleaner than mocking an abstract class. (However I am in the process of migrating all my tests over to PHPUnit for other reasons.)