php - phpunit 功能测试错误调用未定义的方法
问题描述
我有个问题,
我在 symfony 3.4 中使用 phpunit WebTestCase
但我无法选择任何数据
我得到
1) Tests\BankBundle\Controller\BankControllerTest::testmoneyIn 断言 null 匹配预期 1 失败。
这是我的控制器测试
<?php
namespace Tests\BankBundle\Controller;
use BankBundle\Entity\entry;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class BankControllerTest extends WebTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
public function setUp(): void
{
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testmoneyIn()
{
$client = static::createClient();
$client->request('POST', '/bank/moneyin', array('amount' => 50));
$bank = $this->em
->getRepository('BankBundle:entry')
->getId(1);
$this->assertEquals(1, $bank);
}
/**
* {@inheritDoc}
*/
protected function tearDown(): void
{
parent::tearDown();
$this->em->close();
}
}
解决方案
您正试图断言$bank = 1
. 但$bank
为空。你的测试失败了。对于测试使用,您应该始终使用可预测的数据。这意味着,无论何时运行测试,您都知道预期的结果。因此,您确信如果您的断言失败,这意味着您的代码是错误的。