0

我现在开始在一个新项目中使用 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 选择、插入、更新、删除、多个插入等)都有效。我错过了什么?

4

1 回答 1

0

事实证明,正如 WiredPrairie 所说,案件很重要。多么愚蠢的错误……

于 2013-08-14T17:32:15.847 回答