我读过很多关于如何模拟RavenDb的问题。有一个常见的答案:“不要”
这让我陷入了一个奇怪的境地。我模拟接口的一个最重要的原因是测试我的代码对错误的反应。
如果您无法模拟可能导致错误的对象,则注入错误可能会非常复杂。
我是不是想错了方向?
//lg
发布于 2012-04-17 14:16:12
为什么要为了模拟错误而进行模拟呢?创建一个内存中的数据库(使用EmbaddedDocumentStore),只做错误操作,不需要模拟它。
发布于 2015-04-17 23:59:57
我不知道您是否知道,RavenDB在单元测试方面有很好的帮助器。
您唯一需要做的就是实现RavenTestBase,如下所示:
[TestFixture]
public class RavenDummyTests : RavenTestBase
{
private IDocumentStore _documentStore;
[SetUp]
public void Setup()
{
_documentStore = NewDocumentStore();
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
_documentStore.Dispose();
}
[Test]
public void Search_And_Where_Result_In_An_And()
{
using (var db = _documentStore.OpenSession())
{
db.Store(_oscar);
db.Store(_max);
db.Store(_tiger);
db.SaveChanges();
}
WaitForIndexing(_documentStore); // <== very helpful
using (var db = _documentStore.OpenSession())
{
var query = db.Query<Cat>().Search(cat => cat.Color, "gray").Where(cat => cat.Name == "max");
var list = query.ToList();
Assert.IsEmpty(list);
Assert.AreEqual("Color:(gray) AND (Name:max)", query.ToString());
}
}}https://stackoverflow.com/questions/10180044
复制相似问题