Dennis Lanning
Dennis Lanning

Reputation: 49

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. Isn't there a way to construct normally?

I've tried looking this up but all examples use constructs without arguments. Thanks.

Upvotes: 4

Views: 1800

Answers (1)

user3415983
user3415983

Reputation: 111

You are already passing an arguement to your constructor by using $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:

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:

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");
    }
}

Upvotes: 2

Related Questions