每次测试PHPSpec类构造

时间:2015-05-10 18:56:51

标签: php phpspec

我正在寻找一种方法来表示PHPSpec中的每个测试方法,每个测试需要一个像这样创建的类构造函数

$config = new Config( 'path_to_config' );
$client = new SOAPService( $config );
$debtor = new Debtor( $client );

然后我可以测试像

这样的东西
class DebtorSpec extends ObjectBehavior
{
function it_has_method_getDebtor()
{
$debtor->getDebtor( '123' )->shouldReturn( TRUE );
}
}

如何将这种类构造函数传递给phpspec?

1 个答案:

答案 0 :(得分:0)

您可以使用let()方法并在其中调用beConstructedWith()。阅读here

CLASS

class Hello
{
    private $writer;

    public function __construct(Writer $write)
    {
        $this->write = $writer;
    }

    // You methods
}

SPEC

namespace spec;

use PhpSpec\ObjectBehavior;
use Markdown\Writer;

class HelloSpec extends ObjectBehavior
{
    function let(Writer $writer)
    {
        $this->beConstructedWith($writer);
    }

    // Write your method test
}