【问题标题】:PhpSpec expected call exactPhpSpec 预期调用准确
【发布时间】:2014-10-28 10:06:32
【问题描述】:

我有一个用于我的 Laravel5 应用程序的方法,我正在尝试在 PhpSpec 中进行测试,但它一直抛出以下错误:

App/Helpers/RecurrRule                              
37  ! it should return a recurrence collection method call:
    Double\ChurchRepositoryInterface\P6->getChurchTimezone(1) was not expected.
    Expected calls are:
    - getChurchTimeZone(exact(1))

该方法传递一个数组,我想模拟模型的 getChurchTimezone 方法返回一个时区。

方法:

public function __construct(Rule $rule, ArrayTransformer $arrayTransformer, ChurchRepositoryInterface $church)
{
    $this->rule = $rule;
    $this->arrayTransformer = $arrayTransformer;
    $this->church = $church;
}


public function getRecurrenceCollection($schedule)
{
    $timezone    = $this->church->getChurchTimezone($schedule['church_id']);
    // more code
}

在我的其他规范中,我经常做$double->method('someArgument')->willReturn('blah') 之类的事情,但由于某种原因,这种特定情况不起作用,我不知道为什么。

我试过给它一个数组的副本,我试过直接给它一个整数,我试着把 $this->church->getChurchTimezone 拉到它自己的方法中,然后给它一个数组或一个整数,但是无论什么 PhpSpec 总是返回一个“准确”的预期调用错误。

规格:

class RecurrRuleSpec extends ObjectBehavior
{

    protected $schedule = [
        'church_id' => 1,
        'start' => '2014-11-21 10:36:07',
        'end' => '2014-11-21 11:36:07',
        'recurr_rule' => 'FREQ=WEEKLY;BYDAY=SU;',
        'recurr_until' => '2015-02-22 10:36:07',
    ];

    function it_is_initializable()
    {
        $this->shouldHaveType('App\Helpers\RecurrRule');
    }

    /**
     * Setup our mock, with doubles matching our constructor
     */
    function let($rule, $arrayTransformer, $church)
    {
        $rule->beADoubleOf('\Recurr\Rule');
        $arrayTransformer->beADoubleOf('\Recurr\Transformer\ArrayTransformer');
        $church->beADoubleOf('\App\Repositories\ChurchRepositoryInterface');

        $this->beConstructedWith($rule, $arrayTransformer, $church);
    }

    function it_should_return_a_recurrence_collection($rule, $arrayTransformer, $church)
    {
        $church->getChurchTimeZone(1)->willReturn('Europe/Dublin');
        // $church->getChurchTimeZone($this->schedule['church_id'])->willReturn('Europe/Dublin');
        // $church->getChurchTimeZone(Argument::any())->willReturn('Europe/Dublin');
        $rule->createFromString()->willReturn([]);
        $arrayTransformer->transform()->willReturn([]);


        $this->getRecurrenceCollection($this->schedule)->shouldReturn([]);
    }

这是我应用中其他地方的另一个规范示例,它使用相同的方法并通过

function it_should_return_a_one_dimensional_numeric_array_of_servers($config)
{

    $expectedResult = ['domain.com', 'domain2.com'];

    $config->get('servers')->willReturn($this->configGetReturn);
    $this->serverArray()->shouldBeArray();
    $this->serverArray()->shouldEqual($expectedResult);
}

【问题讨论】:

    标签: unit-testing phpspec laravel-5


    【解决方案1】:

    您在规范和代码中对方法名称的拼写不同: getChurchTimeZone / getChurchTimezone

    线索确实在错误信息中:

    getChurchTimezone(1) was not expected.
    
    Expected calls are:
    - getChurchTimeZone(exact(1))
    

    顺便说一句,您可以通过像这样编写 let() 函数来利用 PhpSpec 的一些魔力:

    function let(Rule $rule, ArrayTransformer $arrayTransformer, ChurchRepositoryInterface $church)
    {
        $this->beConstructedWith($rule, $arrayTransformer, $church);
    }
    

    PhpSpec 自动从 let() 签名中的类型提示中找出预期的模拟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2020-03-25
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      相关资源
      最近更新 更多