在我的Symfony4.1项目中,我正在尝试通过单元测试来测试一个方法,该方法应使用SwiftMailer发送电子邮件。
我的测试类如下:
问题是,当我运行
所以我添加了
我的测试类如下:
namespace App\Tests;
use App\Controller\UserImageValidationController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
class UserImageValidationControllerTest extends TestCase
{
private $mailer = null;
public function __construct(\Swift_Mailer $testmySwiftMailer)
{
$this->mailer = $testmySwiftMailer;
}
public function testMail()
{
$controller = new UserImageValidationController();
$controller->notifyOfMissingImage(
'a',
'b',
'c',
'd',
$this->mailer
);
}
}
问题是,当我运行
./bin/phpunit
时,会出现异常,内容为:
看起来在测试环境中DI没有起作用。Uncaught ArgumentCountError: Too few arguments to function App\Tests\UserImageValidationControllerTest::__construct(), 0 [...] and exactly 1 expected [...]
所以我添加了
bind:
$testmySwiftMailer: '@swiftmailer.mailer.default'
我将config/services_test.yaml添加到了我的文件中,但是我仍然得到相同的错误。
我还尝试在该文件中添加了autowiring: true
(只是为了尝试),但仍然不起作用。
另外,我尝试使用服务别名,就像文件中所述:仍然没有成功。
我该如何将SwiftMailer注入到我的测试用例构造函数中?