使用MongoDB进行单元测试 [英] Unit testing with MongoDB

查看:1359
本文介绍了使用MongoDB进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择的数据库是MongoDB。我正在编写一个数据层API来从客户端应用程序中抽象实现细节 - 也就是说,我实际上提供了一个公共接口(一个充当IDL的对象)。

My database of choice is MongoDB. I'm writing a data-layer API to abstract implementation details from client applications - that is, I'm essentially providing a single public interface (an object which acts as an IDL).

我正在测试我的逻辑,因为我采用TDD方式。在每个单元测试之前,调用 @Before 方法来创建数据库单例,之后,当测试完成时, @After 方法来删除数据库。这有助于提高单元测试之间的独立性。

I'm testing my logic as I go in a TDD manner. Before each unit test, an @Before method is called to create a database singleton, after which, when the test completes, an @After method is called to drop the database. This helps to promote independence among unit tests.

几乎所有单元测试,即执行上下文查询,都需要某种插入逻辑才能发生在手之前。我的公共接口提供了一个insert方法 - 然而,使用这个方法作为每个单元测试的前驱逻辑似乎是不正确的。

Nearly all unit tests, i.e. performing a contextual query, require some kind of insertion logic to occur before hand. My public interface provides an insert method - yet, it seems incorrect to use this method as precursor logic to each unit test.

我真的需要某种模拟机制,但是我对模拟框架没有多少经验,似乎Google没有返回任何可能与MongoDB一起使用的模拟框架。

Really I need some kind of mocking mechanism, yet, I haven't had much experience with mocking frameworks, and it seems that Google returns nothing re a mocking framework one might use with MongoDB.

其他人在这些方面做了什么情况呢?也就是说,人们如何对与数据库交互的代码进行单元测试?

What do others do in these situations? That is, how do people unit test code that interacts with a database?

此外,我的公共接口连接到外部配置文件中定义的数据库 - 它似乎不正确使用这个连接进行我的单元测试 - 再次,这种情况可以从某种模拟中受益?

Also, my public interface connects to a database defined in a external configuration file - it seems incorrect to use this connection for my unit testing - again, a situation that would benefit from some kind of mocking?

推荐答案

正如sbridges写的那样这篇文章不提供专用服务(有时也称为存储库或DAO),从逻辑中抽象出数据访问是一个坏主意。然后你可以通过提供一个模拟DAO来测试逻辑。

As sbridges wrote in this post it is a bad idea not to have a dedicated service (sometimes also known as repository or DAO) which abstracts the data access from the logic. Then you could test the logic by providing a mock of the DAO.

我做的另一种方法是创建一个Mongo对象的模拟(例如PowerMockito),然后返回适当的结果。
这是因为您不必测试数据库是否在单元测试中工作,但是您应该测试是否将正确的查询发送到数据库。

Another approach which I do is to create a Mock of the Mongo object (e.g. PowerMockito) and then return the appropriate results. This because you don't have to test if the database works in unit tests but more over you should test if the right query was sent to the databse.

Mongo mongo = PowerMockito.mock(Mongo.class);
DB db = PowerMockito.mock(DB.class);
DBCollection dbCollection = PowerMockito.mock(DBCollection.class);

PowerMockito.when(mongo.getDB("foo")).thenReturn(db);
PowerMockito.when(db.getCollection("bar")).thenReturn(dbCollection);

MyService svc = new MyService(mongo); // Use some kind of dependency injection
svc.getObjectById(1);

PowerMockito.verify(dbCollection).findOne(new BasicDBObject("_id", 1));

这也是一种选择。当然,模拟的创建和适当对象的返回仅作为上面的例子编码。

That would also be an option. Of course the creation of the mocks and returning of the appropriate objects is just coded as an example above.

这篇关于使用MongoDB进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆