单元测试 MongoDB WriteOneAsync() 无法创建文档
本文关键字:创建 文档 MongoDB WriteOneAsync 单元测试 | 更新日期: 2023-09-27 18:33:36
我有点困惑,写一个单元测试来测试MongoDB WriteOneAsync()。我尝试在MongoDB示例上运行单元测试,该示例将文档写入MongoDB,但无法将文档写入数据库。
下面是我的代码。我尝试等待 mc.savetomongo();但它不能等待一个空白。
这只是我为演示问题而整理的一个例子。我的实际代码使用接口和数据层,但同样的问题。
我应该说,如果我从它自己的项目类或表单项目中调用 savetomongo(),代码工作正常。该问题似乎仅在单元测试时才存在。
任何帮助
都非常感谢!非常感谢
我有一个单元测试项目如下,
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using mongotestclass;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public async void TestMethod1()
{
Mongoclass mc = new Mongoclass();
mc.savetomongo();
}
}
}
我还有一个单独的类项目,其中包含以下内容。
using MongoDB.Bson;
using MongoDB.Driver;
using System;
namespace mongotestclass
{
public class Mongoclass
{
protected static IMongoClient _client;
protected static IMongoDatabase _database;
public async void savetomongo()
{
Uri con;
Uri.TryCreate("mongodb://" + "DESKTOP-263RHTL:27017", UriKind.Absolute, out con);
_client = new MongoClient(new MongoClientSettings
{
ConnectTimeout = new TimeSpan(0, 0, 0, 5, 0),
Server = new MongoServerAddress(con.Host, con.Port)
});
_database = _client.GetDatabase("SchemaTest3");
var document = new BsonDocument
{
{ "address" , new BsonDocument
{
{ "street", "2 Avenue" },
{ "zipcode", "10075" },
{ "building", "1480" },
{ "coord", new BsonArray { 73.9557413, 40.7720266 } }
}
},
{ "borough", "Manhattan" },
{ "cuisine", "Italian" },
{ "grades", new BsonArray
{
new BsonDocument
{
{ "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "A" },
{ "score", 11 }
},
new BsonDocument
{
{ "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "B" },
{ "score", 17 }
}
}
},
{ "name", "Vella" },
{ "restaurant_id", "41704620" }
};
var collection = _database.GetCollection<BsonDocument>("restaurants");
await collection.InsertOneAsync(document);
}
}
}
问题在于您的测试过早结束,因为您没有等待任何异步任务。因此,在插入文档和终止测试之间存在争用条件。
您实际需要做的是公开一个async Task
操作,而不是async void
,这将允许您await
测试的调用链:
public Task SaveToMongoAsync()
{
Uri con;
Uri.TryCreate("mongodb://" + "DESKTOP-263RHTL:27017", UriKind.Absolute, out con);
_client = new MongoClient(new MongoClientSettings
{
ConnectTimeout = new TimeSpan(0, 0, 0, 5, 0),
Server = new MongoServerAddress(con.Host, con.Port)
});
_database = _client.GetDatabase("SchemaTest3");
var document = new BsonDocument
{
{ "address" , new BsonDocument
{
{ "street", "2 Avenue" },
{ "zipcode", "10075" },
{ "building", "1480" },
{ "coord", new BsonArray { 73.9557413, 40.7720266 } }
}
},
{ "borough", "Manhattan" },
{ "cuisine", "Italian" },
{ "grades", new BsonArray
{
new BsonDocument
{
{ "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "A" },
{ "score", 11 }
},
new BsonDocument
{
{ "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "B" },
{ "score", 17 }
}
}
},
{ "name", "Vella" },
{ "restaurant_id", "41704620" }
};
var collection = _database.GetCollection<BsonDocument>("restaurants");
return collection.InsertOneAsync(document);
}
现在让你的测试也async Task
并await
其中:
[TestClass]
public class UnitTest1
{
[TestMethod]
public async Task TestMongoInsertionAsync()
{
Mongoclass mc = new Mongoclass();
await mc.SaveToMongoAsync();
}
}
作为旁注,我会改进这个测试,并实际返回一个可以在测试中断言的值,以确保正确插入文档。