简体   繁体   English

如何传递参数来构建phpspec?

[英]how do I pass arguments to construct from a phpspec?

if I have 如果我有

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

it works great, but how do I actually pass arguments to the construct? 它工作得很好,但我如何实际将参数传递给构造? It only works if I have a construct with no arguments passed to it, and use setters after construction. 它只有在我没有传递参数的构造时才有效,并且在构造之后使用setter。 Isn't there a way to construct normally? 有没有正常建设的方法?

I've tried looking this up but all examples use constructs without arguments. 我试过这个,但是所有的例子都使用没有参数的构造。 Thanks. 谢谢。

You are already passing an arguement to your constructor by using $this->beConstrutedWith($bar) 你已经通过使用$ this-> beConstrutedWith($ bar)向你的构造函数传递了一个争论

The first example that you run, using a method from your SUT, will cause the constructor of the class to be called with $bar: 使用SUT中的方法运行的第一个示例将导致使用$ bar调用类的构造函数:

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

Here is the most up-to-date documenation on let() http://phpspec.readthedocs.org/en/latest/cookbook/construction.html#using-the-constructor : 以下是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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM