行为驱动型开发正在使PHPSpec失去我的期望

时间:2016-10-13 13:16:19

标签: php phpspec

我通过PHPSpec生成了以下类:

class Consumer
{
   public function __construct($accesskey, $accessToken)
   {
      // TODO: write logic here
   }
}

当我测试构造函数时,我得到一个错误,它缺少参数1.以下是我编写行为的方法:

namespace spec\Zizy\Aggregator\Context;

use Zizy\Aggregator\Context\Contract\ContextContractInterface;
use Zizy\Aggregator\Context\Consumer;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

 class ConsumerSpec extends ObjectBehavior
 {
    function it_is_initializable()
    {
      $this->beConstructedWith( md5('samplekey'), md5('sampletoken') );
      $this->shouldHaveType(Consumer::class);
    }

    /**
    *  This spec describes how we would access our consumer directry
    */
    public function it_gets_access_token()
    {
       $this->getAccessToken()->shouldReturn(md5('sampletoken'));
    }
 }

以下是运行PHPSpec时出现的错误。

Zizy\Aggregator\Context\Consumer 21  - it gets access token
  warning: Missing argument 1 for Zizy\Aggregator\Context\Consumer::__construct() in C:\wamp64\www\spikes\src\Context\Consumer.php line 7

我也尝试通过接口测试我的消费者,但是PHPSpec一直告诉我它找不到接口但是在类上下文中因此为我提供了创建类的机会,同时它实际上应该是一个接口。

如何通过PHPSpec接口编写代码?

1 个答案:

答案 0 :(得分:1)

您需要为每个示例案例指定构造函数参数。如果您发现有点过于费力,可以使用let在每个示例运行之前做好准备。对于你的情况,这样的事情应该有效:

namespace spec\Zizy\Aggregator\Context;

use Zizy\Aggregator\Context\Contract\ContextContractInterface;
use Zizy\Aggregator\Context\Consumer;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ConsumerSpec extends ObjectBehavior
{
    function let()
    {
        $this->beConstructedWith( md5('samplekey'), md5('sampletoken') );
    }

    function it_is_initializable()
    {
        $this->shouldHaveType(Consumer::class);
    }

    /**
    *  This spec describes how we would access our consumer directry
    */
    public function it_gets_access_token()
    {
        $this->getAccessToken()->shouldReturn(md5('sampletoken'));
    }
}