我有一個服務,它應該創建一個電子郵件類對象並將它傳遞給第三個類(email-sender)。如何從存根函數參數中獲取屬性?
我想檢查由函數生成的電子郵件的正文。
Service.php
class Service
{
/** @var EmailService */
protected $emailService;
public function __construct(EmailService $emailService)
{
$this->emailService = $emailService;
}
public function testFunc()
{
$email = new Email();
$email->setBody('abc'); // I want to test this attribute
$this->emailService->send($email);
}
}
Email.php:
class Email
{
protected $body;
public function setBody($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
}
EmailService.php
interface EmailService
{
public function send(Email $email);
}
所以我創建emailService和電子郵件stub類。但我無法驗證電子郵件的正文。我也無法檢查$的電子郵件 - > setBody()被調用,因爲測試的功能
class ServiceSpec extends ObjectBehavior
{
function it_creates_email_with_body_abc(EmailService $emailService, Email $email)
{
$this->beConstructedWith($emailService);
$emailService->send($email);
$email->getBody()->shouldBe('abc');
$this->testFunc();
}
}
我這裏面創建的電子郵件:
Call to undefined method Prophecy\Prophecy\MethodProphecy::shouldBe() in /private/tmp/phpspec/spec/App/ServiceSpec.php on line 18
在實際應用中產生的體,所以我想測試它是否正確生成。我怎樣才能做到這一點?
謝謝。我真的很感激! – xorik
我上傳了工作代碼:https://gist.github.com/xorik/89da2c3c065d1b6b6d10d915191305f2 – xorik