【发布时间】:2013-04-24 13:26:45
【问题描述】:
如果您有一个根据构造函数参数做出不同响应的类,您将如何为该类编写规范?
class Route
{
function __construct($url, array $methods = array())
{
// stores methods and url in private member variables
// creates a regex to match $url against incoming request URLs
}
public function isMatch($url)
{
// checks if the incoming request url matches against this url
}
}
使用示例:
$a = new Route('/users/:id');
$a->isMatch('/users/1') // returns true;
$b = new Route('/users');
$b->isMatch('/users') // returns true
如果我使用 phpspec 中的 let 函数为这个类设置我的规范:
class Route extends ObjectBehaviour
{
function let()
{
$this->beConstructedWith('/users/:id')
}
}
我的规范只能检查此类的行为是否适用于其中一种情况。
我曾考虑添加 setter 方法以允许我对此进行测试,但似乎我会为了测试目的而打破封装。
我正在努力寻找与此相关的任何内容,所以我开始认为这可能是糟糕的代码异味情况。
【问题讨论】:
-
beConstructedWith() 并不总是必须从 let() 方法调用。您也可以从规范中调用它。