- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我无法访问或使用任何 Eloquent 方法,使用 PHPSpec 有什么意义?
例如:($this 指的是 Eloquent Product
模型)
function it_removes_property(PropertyValueInterface $property)
{
$this->addProperty($property);
$this->properties->shouldHaveCount(1);
$this->removeProperty($property);
$this->properties->shouldHaveCount(0);
}
这不会起作用,因为在方法 addProperty
和 removeProperty
中调用了各种 Eloquent Collection 和 Model 函数,似乎 PHPSpec 无法处理,即使所有这些类包含在 use
语句中。
我注意到在 Jeffery Way 在 Laracasts 上的投屏中,他从未使用过真正的 Eloquent 模型。他只使用 vanilla PHP 对象。那有什么意义呢?那不是现实世界。
此外,这与正确引用 Eloquent 模型类无关,因为我已经这样做了 use Illuminate\Database\Eloquent\Model;
此外,我从不使用外观。所以也不是那样。
最佳答案
PHPSpec 不能做很多您可以做的事情,例如,用 PHPUnit 和 Mockery 做。
底线:我认为 PHPSpec 不是测试 Eloquent 的正确工具。
Eloquent 内部发生了很多“魔法”,而 PHPSpec 似乎并不喜欢魔法,如果你觉得你必须使用 PHPSpec 来测试 Eloquent,否则世界就会 splinter ,那么这里是您可以做几件事。
免责声明: 我不是鼓励你继续使用 PHPSpec 进行 Eloquent 测试,事实上我不希望你用它来测试 Eloquent 模型,我只是解释一些在测试魔术方法和魔法时解决您将遇到的情况的技巧 - 希望您能够在有意义的时候将它们应用到其他地方。对我来说,在 Eloquent 模型的情况下没有意义。
所以这是列表:
getAttribute()
和 setAttribute()
$user->profile
。使用方法 $user->profile()->getResults()
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
}
通过在诱饵类上定义 where
和 latest
方法并使其成为 SUT,您让 PHPSpec 知道这些方法确实存在于类中。它们的参数和返回类型并不重要,重要的是存在。
优势 ?
现在在您的规范中,当您在模型上调用 ->where()
或 ->latest()
方法时,PHPSpec 不会提示它,您可以更改方法诱饵类返回一个 Prophecy
的对象并对其进行断言。
关于PHPSpec 和 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489636/
我已经开始了一个 composer 项目并包含了 phpspec-st, 当我运行命令时: /var/www/yii-video-site/vendor$ bin/phpspec desc video
我正在使用 PHPSpec,但每当测试失败时,我都会得到以下信息: 它确实说明了我的测试失败的地方,但它没有输出确切返回的内容,因此没有帮助。 我试图通过创建一个 phpspec.yml 文件并将其放
我决定使用 BDD 工作流编写简单的 MVC。我想实现设置类的某些属性的方法。这件事与文件路径有关。在这种方法中,我想检查路径是否正确以及该文件是否存在。最终产品应如下所示: someProperty
我对 phpspec 还很陌生,但通常当我遇到问题时我会找到解决方案,但这个很难。 我尝试了很多不同的方法,但我还没有找到解决方案。我正在使用 Symfony2。 我有一个要测试的类: class M
我想获取对象的实际 返回值,而不是可链接的对象。 class Foo { public $attribute = 'data'; } class FooSpec extends ObjectBe
我有一个如下所示的 phpspec 类,我只想从中运行一个规范。我只想运行“it_should_do_something_easy”。这可能吗? 这就是我运行这个规范文件的方式: $> phpspec
我在项目中将 Doctrine 2.5 更新为 2.6,但 phpspec 已损坏。 函数 getEntityChangeSet() 现在通过引用返回。 phpspec好像不支持。 $unitOfWo
在 phpspec 中,我可以模拟方法的返回值吗? 例如: class MyClass() { public function getStaffMemberNames() {
我有一个简单的测试: function it_should_return_error_response_exception(Client $httpClient,CommandInterface $c
有人知道从 PHPSpec 测试生成覆盖率报告的方法吗? 我考虑过 xdebug,但据我所知它不能为 jenkins 生成报告。 最佳答案 就目前 (1.4.0) 而言,它还不支持代码覆盖率。很高兴听
如果我无法访问或使用任何 Eloquent 方法,使用 PHPSpec 有什么意义? 例如:($this 指的是 Eloquent Product 模型) function it_removes_pr
我很好奇指定处理文件操作的类的最佳方式。 假设我有一个虚构的类,其方法duplicate 的工作是复制文件的内容。 workDir = vfsStream::setup('workDir');
我不知道这个方法是如何工作的。这是我尝试使用它的示例: namespace spec; use PhpSpec\ObjectBehavior; class MyObjectSpec extends O
当我尝试创建它时,我在我的测试类中看到了警告。我不想为测试类创建 PHPDoc 和更多内容,但 PhpStorm 向我显示警告,因为 PhpStorm 无法将测试类标记为排除 phpcs。 我如何运行
使用 query_builder 的anonymous function 测试此表单的正确方法是什么? use Doctrine\ORM\EntityRepository; // ... $build
我肯定我漏掉了一些明显的东西。但是我在安装和使用 phpspec 时遇到问题。首先我不知道我应该使用 phpspec 还是 phpspec2。 我试过这样安装:- PEAR(不起作用)- git hu
我正在使用 Windows。我用 composer 安装了 phpspec 包,我将文件夹 phpspec/bin/添加到我系统的路径变量中。但是当我尝试运行 phpspec 命令时,出现以下错误:
有没有一种方法可以测试 getNumberOfProductsForCategory 方法将返回一个整数而不是一个特定值(在本例中为 18)? 我觉得简单地测试“18”虽然现在是正确的,但会使我的测试
过去我总是偶然发现 phpspec 的某个问题: 假设我有一个方法调用另一个对象的多个方法 class Caller { public function call(){ $thi
假设我有一个 RuleFactory 的这个方法: public function makeFromArray($rules) { $array = []; foreach ($rul
我是一名优秀的程序员,十分优秀!