我现在开始在一个新项目中使用MongoDB。我为一个名为PersistanceManager<C>
的类创建了一些测试。这个类是MongoDB的一些功能的包装器。它现在非常简单,随着我们需要更多功能,它将会扩展。
问题在于我遇到了一个失败的测试。它失败并显示消息“没有返回对象”。这是测试:
[Test, Description("Tests the selection of an object by an attribute.")]
public void ObjectFieldSelectionTest()
{
InstanceTest it = new InstanceTest(5898, "1234");
PersistanceManager<InstanceTest> pm = new PersistanceManager<InstanceTest>();
pm.Insert(it);
InstanceTest[] its = pm.GetInstanceByAttr(1, new KeyValuePair<string, BsonValue>("field", "1234"));
if (its.Length < 1)
{
pm.Delete(it);
Assert.Fail("No object was returned");
}
else
{
InstanceTest it2 = its[0];
pm.Delete(it);
Assert.AreEqual(it.Field, it2.Field);
}
}
我从PersistantManager<C>
测试的方法就是这个:
private MongoCollection<C> collection;
public PersistanceManager()
{
MongoDatabase db = new MongoClient("mongodb://localhost").GetServer().GetDatabase("db");
collection = db.GetCollection<C>(typeof(C).Name);
}
//This is the method
public C[] GetInstanceByAttr(int amount, KeyValuePair<string, BsonValue> kvp)
{
MongoCursor<C> cursor = collection.FindAs<C>(Query.EQ(kvp.Key, kvp.Value));
cursor.Limit = amount;
return cursor.ToArray<C>();
}
我用于测试的类InstanceTest
如下(它与测试在同一个类中定义):
private class InstanceTest: IDBStorable
{
public BsonValue Id { get; set; }
public string Field { get; set; }
public InstanceTest() { }
public InstanceTest(int id, string field)
{
this.Id = id;
this.Field = field;
}
}
所有其他测试(通过id,inset,update,delete,multiple inset等选择)都有效。 我错过了什么?
答案 0 :(得分:0)
事实证明,正如WiredPrairie所说,案件很重要。多么愚蠢的错误......