我在试着测试一堂课是否是期末课程。因为我还没有找到默认匹配程序(或者任何其他干净的测试方法),所以我决定创建一个自定义扩展,它添加了一个新的匹配器来完成这个任务,但是我无法让它工作。
我试过用内嵌式匹配器,就像这样:
public function getMatchers(): array
{
    return [
        'beFinal' => function ($subject) {
            $reflection = new \ReflectionClass($subject);
            if (!$reflection->isFinal()) {
                throw new FailureException('Expected subject to be final, but it is not.');
            }
            return true;
        },
    ];
}当我调用$this->shouldBeFinal();时,这已经足够好了。问题是,当我调用$this->shouldNotBeFinal();时,它输出一个通用消息:[obj:Class\To\Test] not expected to beFinal(), but it did.,而不是我想要显示的消息。
另一个问题是,我不想只在一节课上这样做。这就是为什么我决定延长它的期限。
我得到的是:
phpspec.yml
extensions:
    PhpSpecMatchers\Extension: ~PhpSpecMatchers/Extension.php
<?php
declare(strict_types=1);
namespace PhpSpecMatchers;
use PhpSpec\ServiceContainer;
use PhpSpecMatchers\Matchers\BeFinalMatcher;
class Extension implements \PhpSpec\Extension
{
    public function load(ServiceContainer $container, array $params): void
    {
        $container->define(
            'php_spec_matchers.matchers.be_final',
            function ($c) {
                return new BeFinalMatcher();
            },
            ['matchers']
        );
    }
}PhpSpecMatchers/Matchers/BeFinalMatcher.php
<?php
declare(strict_types=1);
namespace PhpSpecMatchers\Matchers;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\Matcher\BasicMatcher;
class BeFinalMatcher extends BasicMatcher
{
    public function supports(string $name, $subject, array $arguments): bool
    {
        return $name === 'beFinal';
    }
    public function getPriority(): int
    {
        return 0;
    }
    protected function matches($subject, array $arguments): bool
    {
        $reflection = new \ReflectionClass($subject);
        return $reflection->isFinal();
    }
    protected function getFailureException(string $name, $subject, array $arguments): FailureException
    {
        return new FailureException('Expected subject to not be final, but it is.');
    }
    protected function getNegativeFailureException(string $name, $subject, array $arguments): FailureException
    {
        return new FailureException('Expected subject to be final, but it is not.');
    }
}每当我试图使用此配置调用$this->beFinal();时,规范就会中断,并显示以下消息:method [array:2] not found.。例如,如果我将一个isFinal()方法添加到我测试的类中并返回true,它将传递给$this->shouldBeFinal();,而对于$this->shouldNotBeFinal();失败,但我不想添加该方法。我应该工作没有它,就我所理解的,它应该能够这样工作,对吗?
我还尝试在我的phpspec.yml,中添加自定义套件,如下所示:
suites:
    matchers:
        namespace: PhpSpecMatchers\Matchers
        psr4_prefix: PhpSpecMatchers\Matchers
        src_path: src/PhpSpecMatchers/Matchers
        spec_prefix: spec/PhpSpecMathcers/Matchers但这改变不了什么。我还尝试将以下配置添加到phpspec.yml中
extensions:
    PhpSpecMatchers\Extension:
        php_spec_matchers:
        src_path: src
        spec_path: spec这也改变不了什么。
我尝试过的另一件事是放弃扩展方法,在phpspec.yml,中声明我的母亲,如下所示:
matchers:
    - PhpSpecMatchers\Matchers\BeFinalMatcher正如你所预期的:同样的结果。
PhpSpecMatchers\Extension中的加载确实会被调用(由一个简单的var_dump(…);测试),但是它似乎没有到达PhpSpecMatchers\Matchers\BeFinalMatcher,中的任何东西,因为我没有从任何var_dump(…);获得任何输出
我学习过symfonycasts、phpspec文档本身和其他github项目的教程和例子,它们与我的代码几乎完全相同(除了名称空间、目录结构和类似的东西),所以我有点不知所措。
如何才能成功地调用$this->shouldBeFinal();和$this->shouldNotBeFinal();
非常感谢谁能帮我。
P.S.:我还在phpspec的github上发布了这个问题。
发布于 2019-03-22 07:43:24
因此,显然优先级太低了(参见这句话我的phpspec的github问题)。PhpSpec\Matcher\IdentityMatcher (来自shouldBe的地方)从优先级设置为100的PhpSpec\Matcher\BasicMatcher扩展而来。因为我的被设置为0,所以它首先是我的(我想),因此没有正确地执行。我已经把我的优先级设定为101,它完美地发挥了作用(除了我改变了积极和消极的信息,我发现)。
https://stackoverflow.com/questions/55282494
复制相似问题