简体   繁体   English

错误:单元测试时无法实例化接口 symfony

[英]Error: Cannot instantiate interface symfony when unit testing

I'm trying to do a unit test for a signup method and im following this guide .我正在尝试对注册方法进行单元测试,并且我正在遵循本指南 I'm fairly new to unit testing.我对单元测试相当陌生。

i keep getting我不断得到

1) App\\Tests\\Controller\\SignUpControllerTest::testSignUp Error: Cannot instantiate interface Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface 1) App\\Tests\\Controller\\SignUpControllerTest::testSignUp 错误:无法实例化接口 Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface

/Applications/MAMP/htdocs/my_project/tests/Controller/SignUpControllerTest.php:19 /Applications/MAMP/htdocs/my_project/tests/Controller/SignUpControllerTest.php:19

I just don't think im doing this unit test right.我只是不认为我做这个单元测试是正确的。 Here is what i have.这就是我所拥有的。 I'm not sure on what im doing.我不确定我在做什么。 All i want to do is test the signup method.我要做的就是测试注册方法。

UserController.php用户控制器.php

public function signup(Request $request, UserPasswordEncoderInterface $passwordEncoder )
{
    $user = new User();

    $entityManager = $this->getDoctrine()->getManager();

    $user->setEmail($request->get('email'));
    $user->setPlainPassword($request->get('password'));
    $user->setUsername($request->get('username'));
    $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
    $user->setPassword($password);

    $entityManager->persist($user);
    $entityManager->flush();

    return $this->redirectToRoute('login');



}

SignUpControllerTest.php SignUpControllerTest.php

namespace App\Tests\Controller;

use App\Entity\Product;
use App\Controller\UserController;
use App\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class SignUpControllerTest extends WebTestCase
{

    public function testSignUp()
    {   
        $passwordEncoder = new UserPasswordEncoderInterface();
        $user = new User();
        $user->setEmail('janedoe123@aol.com');
        $user->setPlainPassword('owlhunter');
        $user->setUsername('BarnMan');
        $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($password);

        $userRepository = $this->createMock(ObjectRepository::class);
        $userRepository->expects($this->any())
            ->method('find')
            ->willReturn($user);


        $objectManager = $this->createMock(ObjectManager::class);
        // use getMock() on PHPUnit 5.3 or below
        // $objectManager = $this->getMock(ObjectManager::class);
        $objectManager->expects($this->any())
            ->method('getRepository')
            ->willReturn($userRepository);

        $userController = new UserController($objectManager);
        $this->assertEquals(2100, $userController->signupTest());

    }


}

The error is very clear. 错误非常明显。 In the first line of your testSignUp method, you are creating an instance out of an interface, which cannot be done in PHP. 在testSignUp方法的第一行中,您是在接口外创建实例,而该实例无法在PHP中完成。

To create a usable object out of an interface in unit testing, create a mock object of it. 要在单元测试中从接口创建可用的对象,请为其创建一个模拟对象。 Read PHP unit docs for that. 请阅读PHP单元文档。

WebTestCase :网络测试案例

Application tests are PHP files that typically live in the tests/Controller/ directory of your application it tell Symfony that we need the tools necessary to talk to our application as if we were doing so via HTTP.应用程序测试是 PHP 文件,通常位于应用程序的 tests/Controller/ 目录中,它告诉 Symfony 我们需要必要的工具来与我们的应用程序通信,就好像我们通过 HTTP 进行通信一样。

KernelTest内核测试

use for to fetch a service from the dependency injection container it help you creating and booting the kernel in your tests用于从依赖注入容器中获取服务,它可以帮助您在测试中创建和启动内核

that's why I recommend you to use KernelTestCase这就是为什么我建议您使用 KernelTestCase

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class SignUpControllerTest extends KernelTestCase
{
private $passwordEncoder;

protected function testSignUp(): void
{
   self::bootKernel();
    $this->passwordEncoder= self::getContainer()->get('Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface');
}

public function addShoes3()
{
     $passwordEncoder = $this->passwordEncoder;
    //continue
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony2单元测试无法实例化测试类中的类 - Symfony2 unit testing cannot instantiate class in test class php+symfony - phpunit 测试 - 无法实例化接口错误 - php+symfony - phpunit test - Cannot instantiate interface error Symfony2无法在依赖注入中实例化接口 - Symfony2 Cannot instantiate interface in dependency injection 在生产环境中安装Magento 2.1.11时无法实例化接口错误 - Cannot instantiate interface error when installing Magento 2.1.11 on production Symfony2表单事件侦听器和数据转换器错误无法实例化接口Doctrine \\…\\ ObjectManager - Symfony2 Form Events Listener and Data Transformer Error Cannot instantiate interface Doctrine\…\ObjectManager 使用Symfony 2和数据库进行单元测试 - Unit testing with Symfony 2 and database Drupal 8-致命错误:无法实例化接口Drupal \\ Core \\ Cache \\ CacheBackendInterface - Drupal 8 - Fatal error: Cannot instantiate interface Drupal\Core\Cache\CacheBackendInterface 从命令分派作业时无法实例化界面 - Cannot instantiate interface when dispatching job from a command 单元测试 Laravel 5 时无法测试重定向 - Cannot test redirects when unit testing Laravel 5 Symfony3单元测试表格在无效数据时始终有效 - Symfony3 unit testing forms always valid when invalid data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM