如何测试使用抽象类的插件接口?

发布于 2024-09-29 07:14:30 字数 588 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-10-06 07:14:30

不确定 simpletest,但 PHPUnit 可以创建抽象类的模型,允许您直接测试它们。

Not sure about simpletest, but PHPUnit can create mockups of abstract classes, that allow you to test them directly.

絕版丫頭 2024-10-06 07:14:30

我决定采用通过创建一个空插件类来测试接口的想法。对我来说,这似乎比模拟抽象类干净得多。 (但是,出于其他原因,我正在将所有测试迁移到 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文