PHPSpec和Laravel
如果我无法访问或使用任何有说服力的方法,那么使用PHPSpec有什么意义? 例如:($this表示有说服力的PHPSpec和Laravel,php,testing,laravel,bdd,phpspec,Php,Testing,Laravel,Bdd,Phpspec,如果我无法访问或使用任何有说服力的方法,那么使用PHPSpec有什么意义? 例如:($this表示有说服力的产品模型) 这不会在方法addProperty和removeProperty中起作用,因为存在对各种有说服力的集合和模型函数的调用,因此PHPSpec似乎无法处理此问题,即使use语句中包含了所有这些类 我注意到在杰弗里·韦(Jeffery Way)的《拉腊卡斯特》(Laracasts)的银幕上,他从未使用过真正雄辩的模型。他只使用普通的PHP对象。那有什么意义?这不是真实的世界 另外,这
产品
模型)
这不会在方法addProperty
和removeProperty
中起作用,因为存在对各种有说服力的集合和模型函数的调用,因此PHPSpec似乎无法处理此问题,即使use
语句中包含了所有这些类
我注意到在杰弗里·韦(Jeffery Way)的《拉腊卡斯特》(Laracasts)的银幕上,他从未使用过真正雄辩的模型。他只使用普通的PHP对象。那有什么意义?这不是真实的世界
另外,这与正确引用eloquent模型类没有任何关系,因为我已经在做use-illumb\Database\eloquent\model代码>
而且我从不使用正面。所以也不是这样。PHPSpec不能做很多你可以做的事情,例如,用PHPUnit和mocky。
一句话:我认为PHPSpec不是测试雄辩的正确工具
在Eloquent内部有很多“魔法”,PHPSpec似乎不喜欢魔法,如果你觉得你必须使用PHPSpec来测试Eloquent,否则世界会崩溃,那么这里有一些你可以做的事情
免责声明:我不鼓励您继续使用PHPSpec进行雄辩测试,事实上我不希望您使用它测试雄辩的模型,我只是解释了一些技巧来解决你在测试魔术和黑魔法时遇到的情况,希望你能在其他地方应用它们。对我来说,这在雄辩的模型中是没有意义的
下面是清单:
- 不要使用神奇的getter和setter,而是使用
getAttribute()
和setAttribute()
- 不要对延迟加载的关系使用魔法调用,即
$user->profile
。使用方法$user->profile()->getResults()
- 创建一个扩展模型的SUT模拟类,并定义模型上的
where
方法,还定义范围方法以及Eloquent“神奇地”为您所做的一切
- 使用
beAnInstanceOf()
方法切换到mock并对其进行断言
下面是我的测试的示例:
产品型号
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
}
产品型号规格
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
}
PHPSpec不能做很多你可以做的事情,例如,使用PHPUnit和mocky。
一句话:我认为PHPSpec不是测试雄辩的正确工具
在Eloquent内部有很多“魔法”,PHPSpec似乎不喜欢魔法,如果你觉得你必须使用PHPSpec来测试Eloquent,否则世界会崩溃,那么这里有一些你可以做的事情
免责声明:我不鼓励您继续使用PHPSpec进行雄辩测试,事实上我不希望您使用它测试雄辩的模型,我只是解释了一些技巧来解决你在测试魔术和黑魔法时遇到的情况,希望你能在其他地方应用它们。对我来说,这在雄辩的模型中是没有意义的
下面是清单:
- 不要使用神奇的getter和setter,而是使用
getAttribute()
和setAttribute()
- 不要对延迟加载的关系使用魔法调用,即
$user->profile
。使用方法$user->profile()->getResults()
- 创建一个扩展模型的SUT模拟类,并定义模型上的
where
方法,还定义范围方法以及Eloquent“神奇地”为您所做的一切
- 使用
beAnInstanceOf()
方法切换到mock并对其进行断言
下面是我的测试的示例:
产品型号
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
}
产品型号规格
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
}
PHPSpec不能做很多你可以做的事情,例如,使用PHPUnit和mocky。
一句话:我认为PHPSpec不是测试雄辩的正确工具
在Eloquent内部有很多“魔法”,PHPSpec似乎不喜欢魔法,如果你觉得你必须使用PHPSpec来测试Eloquent,否则世界会崩溃,那么这里有一些你可以做的事情
免责声明:我不鼓励您继续使用PHPSpec进行雄辩测试,事实上我不希望您使用它测试雄辩的模型,我只是解释了一些技巧来解决你在测试魔术和黑魔法时遇到的情况,希望你能在其他地方应用它们。对我来说,这在雄辩的模型中是没有意义的
下面是清单:
- 不要使用神奇的getter和setter,而是使用
getAttribute()
和setAttribute()
- 不要对延迟加载的关系使用魔法调用,即
$user->profile
。使用方法$user->profile()->getResults()
- 创建一个扩展模型的SUT模拟类,并定义模型上的
where
方法,还定义范围方法以及Eloquent“神奇地”为您所做的一切
- 使用
beAnInstanceOf()
方法切换到mock并对其进行断言
下面是我的测试的示例:
产品型号
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
}
产品型号规格
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
}
PHPSpec不能做很多你可以做的事情,例如,使用PHPUnit和mocky。
一句话:我认为PHPSpec不是测试雄辩的正确工具
有很多“魔术”发生在雄辩和PHPSpec似乎不喜欢魔术,如果你