dongxi0523 2016-09-14 18:40
浏览 34

测试Symfony Doctrine连接重置的最佳实践

I'm just looking for advice on how the best way to approach the following method, for testing. Should I mock the EntityManager, or pass a real EntityManager in?

I'm sorry if this is basic testing; but I'm fairly new to testing, and really want to do it right.

/**
 * @param EntityManager $em
 * @param $dbName
 * @param $dbUser
 * @param $dbPassword
 *
 * @throws \Exception
 */
public function resetConnection($em, $dbName, $dbUser, $dbPassword)
{
    try {
        $conn = array(
            'driver'   => 'pdo_mysql',
            'user'     => $dbUser,
            'password' => $dbPassword,
            'dbname'   => $dbName
        );

        $new = \Doctrine\ORM\EntityManager::create(
            $conn,
            $em->getConfiguration(),
            $em->getEventManager()
        );

        return $new;
    }
    catch (\Exception $e) {
        throw $e;
    }

    return false;
}

Currently, I built a mock version of an EntityManager object and also created a mock of Configuration and EventManager objects.

I have a test that checks if the exception triggers; but I want to build some better tests to actually check if the exception triggers properly when the new database doesn't exist; the mock doesn't seem to be stable enough for testing this...

Ideas?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探