如果我不能访问或使用任何有说服力的方法,使用PHPSpec有什么意义?
例如:($this reffers to一个出色的Product模型)
function it_removes_property(PropertyValueInterface $property)
{
$this->addProperty($property);
$this->properties->shouldHaveCount(1);
$this->removeProperty($property);
$this->properties->shouldHaveCount(0);
} 这将无法工作,因为在方法addProperty和removeProperty中有对各种雄辩的集合和模型函数的调用,似乎PHPSpec无法处理这个问题,即使所有这些类都包含在use语句中。
我注意到,在Jeffery Way对Laracasts的银幕上,他从未使用过真正雄辩的模型。他只使用普通的PHP对象。这有什么意义?那不是真实的世界。
而且,这与正确引用雄辩的模型类无关,因为我已经在做这个use Illuminate\Database\Eloquent\Model;了
而且我从来没有用过门面。所以也不是那样的。
发布于 2014-12-16 10:01:03
PHPSpec不能做很多你可以做的事情,例如,使用PHPUnit和嘲弄。
一句话:我认为PHPSpec不是测试口才的合适工具。
口才里面有很多‘魔法’,PHPSpec似乎不喜欢魔法,如果你觉得你必须用PHPSpec来测试口才,否则世界会崩溃,那么这里有几件你可以做的事情。
免责声明:我并不鼓励你继续使用PHPSpec进行口才测试,事实上,我不希望你用它来测试雄辩的模型,我只是解释一些技巧来解决你在测试魔法方法和黑色艺术时会遇到的情况--希望你能在有意义的时候将它们应用到其他地方。对我来说,对于雄辩的模特来说,这是没有意义的。
下面是清单:
getAttribute()和setAttribute()$user->profile )的神奇调用。使用方法$user->profile()->getResults()where方法,还定义scope方法和其他所有为您“神奇地”做的事情。beAnInstanceOf()方法切换到模拟并对其进行断言。下面是我的测试的一个示例:
产品模型
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function scopeLatest($query)
{
return $query->where('created_at', '>', new Carbon('-1 week'))
->latest();
}
// Model relations here...
}产品模型规范
<?php namespace Spec\Model;
use Prophecy\Argument;
use App\Entities\Product;
use PhpSpec\ObjectBehavior;
class ProductSpec extends ObjectBehavior
{
public function let()
{
$this->beAnInstanceOf(DecoyProduct::class);
}
public function it_is_initializable()
{
$this->shouldHaveType('Product');
}
}
// Decoy Product to run tests on
class DecoyProduct extends Product
{
public function where();
// Assuming the Product model has a scope method
// 'scopeLatest' on it that'd translate to 'latest()'
public function latest();
// add other methods similarly
}通过在诱饵类上定义where和latest方法并使其满足要求,您将让PHPSpec知道这些方法实际上存在于该类中。他们的论点和返回类型并不重要,只是存在而已。
优势?
现在,在您的规范中,当您在模型上调用->where()或->latest()方法时,PHPSpec不会抱怨它,您可以将诱饵类上的方法更改为返回(例如,Prophecy的一个对象)并在其上进行断言。
https://stackoverflow.com/questions/27489636
复制相似问题