测试与 MongoDB 交互的抽象文档存储库
Testing Abstract Document Repository which interacts with MongoDB
大家好,
我在设置测试用例时遇到问题。
我有一个连接到 mongodb 的普通 symfony 3 项目。我有多个文档,每个文档都需要一个额外的方法来查询数据库。该方法将获取插入集合中的最后一个文档,称为 findLatestInserted()
.
此特定功能在每个文档存储库中都有重复。所以我决定提取它并创建一个扩展默认 DocumentRepository
的 class BaseDocumentRepository
。我所有的文档存储库仍然有自己的 DocumentRepository class 比方说:CpuInfoRepository
、RamInfoRepository
。这些 classes 确实提供了一些额外的方法来查询 mongodb 数据库和 一个共同点:findLatestInserted()
一切正常,但以防万一我想为此方法编写单元测试findLatestInserted()
。
我有一个名为 prototyping-test 的测试数据库,用于创建文档并查询它并检查结果。之后它会自行清除,因此不会保留任何文档。对于每个存储库,都有一个特定的 url 到 post 数据用于在数据库中创建一个文件。要创建 CpuInfo 集合,您需要 post 数据到 http://localhost:8000/ServerInfo/CreateCpuInfo
。要创建 RamInfo 集合,您需要 post 数据到 http://localhost:8000/ServerInfo/CreateRamInfo
。
下面是我的问题,我将如何编写测试来测试方法 findLatestInserted()
?
这是我到目前为止尝试过的方法:
public function testFindLatestInserted()
{
$client = self::createClient();
$crawler = $client->request('POST',
'/ServerInfo/CreateCpuInfo',
[
"hostname" => $this->hostname,
"timestamp" => $this->timestamp,
"cpuCores" => $this->cpuCores,
"cpu1" => $this->cpu1,
"cpu2" => $this->cpu2
]);
$this->assertTrue($client->getResponse()->isSuccessful());
$serializer = $this->container->get('jms_serializer');
$cpuInfo = $serializer->deserialize($client->getResponse()->getContent(), 'AppBundle\Document\CpuInfo', 'json');
$expected = $this->dm->getRepository("AppBundle:CpuInfo")->find($cpuInfo->getId());
$stub = $this->getMockForAbstractClass('BaseDocumentRepository');
$actual = $this->dm
->getRepository('AppBundle:CpuInfo')
->findLatestInserted();
$this->assertNotNull($actual);
$this->assertEquals($expected, $actual);
}
在第 $actual = $this->dm->getRepository('AppBundle:CpuInfo')->findLatestInserted();
行我卡住了。因为这只会测试 CpuInfo,而也有 RamInfo(以及此处未提及的其他一些 classes)。如何处理此设置?
我特别想在抽象级别 class 而不是具体 classes.
级别上测试方法 findLatestInserted()
请帮帮我!
与其测试整个堆栈,不如集中精力测试 findLatestInserted()
具体 类。
将 MondoDB 存根注入 AppBundle:CpuInfo
并检查是否 findLatestInserted()
returns 预期值。
对 AppBundle:RamInfo
.
做同样的事情
避免测试抽象 类,始终测试具体 类。
将来,您可能决定不继承 BaseDocumentRepository
,并且可能不会注意到 findLatestInserted()
的新实现失败。
大家好,
我在设置测试用例时遇到问题。
我有一个连接到 mongodb 的普通 symfony 3 项目。我有多个文档,每个文档都需要一个额外的方法来查询数据库。该方法将获取插入集合中的最后一个文档,称为 findLatestInserted()
.
此特定功能在每个文档存储库中都有重复。所以我决定提取它并创建一个扩展默认 DocumentRepository
的 class BaseDocumentRepository
。我所有的文档存储库仍然有自己的 DocumentRepository class 比方说:CpuInfoRepository
、RamInfoRepository
。这些 classes 确实提供了一些额外的方法来查询 mongodb 数据库和 一个共同点:findLatestInserted()
一切正常,但以防万一我想为此方法编写单元测试findLatestInserted()
。
我有一个名为 prototyping-test 的测试数据库,用于创建文档并查询它并检查结果。之后它会自行清除,因此不会保留任何文档。对于每个存储库,都有一个特定的 url 到 post 数据用于在数据库中创建一个文件。要创建 CpuInfo 集合,您需要 post 数据到 http://localhost:8000/ServerInfo/CreateCpuInfo
。要创建 RamInfo 集合,您需要 post 数据到 http://localhost:8000/ServerInfo/CreateRamInfo
。
下面是我的问题,我将如何编写测试来测试方法 findLatestInserted()
?
这是我到目前为止尝试过的方法:
public function testFindLatestInserted()
{
$client = self::createClient();
$crawler = $client->request('POST',
'/ServerInfo/CreateCpuInfo',
[
"hostname" => $this->hostname,
"timestamp" => $this->timestamp,
"cpuCores" => $this->cpuCores,
"cpu1" => $this->cpu1,
"cpu2" => $this->cpu2
]);
$this->assertTrue($client->getResponse()->isSuccessful());
$serializer = $this->container->get('jms_serializer');
$cpuInfo = $serializer->deserialize($client->getResponse()->getContent(), 'AppBundle\Document\CpuInfo', 'json');
$expected = $this->dm->getRepository("AppBundle:CpuInfo")->find($cpuInfo->getId());
$stub = $this->getMockForAbstractClass('BaseDocumentRepository');
$actual = $this->dm
->getRepository('AppBundle:CpuInfo')
->findLatestInserted();
$this->assertNotNull($actual);
$this->assertEquals($expected, $actual);
}
在第 $actual = $this->dm->getRepository('AppBundle:CpuInfo')->findLatestInserted();
行我卡住了。因为这只会测试 CpuInfo,而也有 RamInfo(以及此处未提及的其他一些 classes)。如何处理此设置?
我特别想在抽象级别 class 而不是具体 classes.
findLatestInserted()
请帮帮我!
与其测试整个堆栈,不如集中精力测试 findLatestInserted()
具体 类。
将 MondoDB 存根注入 AppBundle:CpuInfo
并检查是否 findLatestInserted()
returns 预期值。
对 AppBundle:RamInfo
.
避免测试抽象 类,始终测试具体 类。
将来,您可能决定不继承 BaseDocumentRepository
,并且可能不会注意到 findLatestInserted()
的新实现失败。