4

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.

1 Answers1

2

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");
    }
}
user3415983
  • 111
  • 1
  • 4
  • 1
    If you are referencing documentation, please put the relevant portions of the documentation into your answer. The link is only viable as part of the answer until it changes. – rfornal Jan 01 '15 at 13:35