4

如果我有

function let(Foo bar)
{
    $this->beConstructedWith($bar);
}

它工作得很好,但我实际上如何将参数传递给构造?它仅在我有一个没有传递给它的参数的构造并在构造后使用设置器时才有效。没有办法正常构建吗?

我试过查找这个,但所有示例都使用不带参数的构造。谢谢。

4

1 回答 1

2

您已经通过使用 $this->beConstrutedWith($bar) 将争论传递给您的构造函数

使用 SUT 中的方法运行的第一个示例将导致使用 $bar 调用类的构造函数:

class Baz 
{
   public function __construct(Foo $bar)
   {
   }
}

这是关于 let() http://phpspec.readthedocs.org/en/latest/cookbook/construction.html#using-the-constructor的最新文档 :

namespace spec;

use PhpSpec\ObjectBehavior;
use Markdown\Writer;

class MarkdownSpec extends ObjectBehavior
{
    function it_outputs_converted_text(Writer $writer)
    {
        $this->beConstructedWith($writer);
        $writer->writeText("<p>Hi, there</p>")->shouldBeCalled();

        $this->outputHtml("Hi, there");
    }
}
于 2015-01-01T13:30:30.030 回答