Unit Test Net core – 使用内存数据库进行 MongoDB 集成测试

huangapple go评论57阅读模式
英文:

Unit Test Net core - Integration test with In memory database for MongoDB

问题

我有一个要使用MongoDb完成的类库项目。我想编写单元测试来覆盖所有的功能。

我正在尝试找到在.NET Core 6.0中编写单元测试时创建MongoDb的内存数据库的方法。

但是我一点头绪都没有。

我可以为MySQL、SQL创建内存数据库,它们运行得很好。但是对于MongoDb,它总是使用我的本地默认端口27017。

能否提供文档或示例?

非常感谢。

英文:

I have a class library project to do with MongoDb. I want to write UTs to cover all functions.

I'm trying to find the way create an InMemory database for MongoDb when write Unit test in net core 6.0.

But I don't have any clue about it.

I can create InMemory database for mysql, sql. It work well. But with MongoDb, It alway use my default port 27017 on my local

Could give me the document or example?

Thanks you a lot.

答案1

得分: 2

MongoDB 无法在客户端上配置为“内存”模式。无论如何,它将始终是客户端与服务器之间的通信,驱动程序将始终使用 Web 套接字与服务器通信。您唯一能够达到的目标是可以在服务器端配置 内存 存储引擎(还请参见此处此处),这实际上意味着服务器将数据保留在内存中(并因此占用更多内存),而不是将数据存储在磁盘上,但就我所知,这不会给您带来任何好处。

英文:

MongoDB cannot be configured as "in memory" mode on a client side. It will be client server communication in any case and a driver will always use a web socket to communicate with server. The only target you can reach is you can configure in memory storage engine on server side (also see here and here) which effectively will mean that instead of storing data on a disk by server, the server will keep it in memory (and consume much more of memory as the result) but it won't give you any benefits as far as I can tell.

huangapple
  • 本文由 发表于 2023年3月7日 17:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定