RavenDB:我如何在单元测试中使用RavenProfiler?
我正在使用NancyFX+RavenDB。我目前正在尝试实现Ayende推荐的技术,从AggregateRoot构建域引用。如果您阅读了该链接,您可以看到诀窍是加载父实例,然后使用RavenDB的“Include”语句预取引用的实例 我已经完成了所有这一切,并且它看起来很有效,但是我正在努力通过单元测试来确定引用的实例是否真的是预取的。下面是我的单元测试的一个片段来说明:RavenDB:我如何在单元测试中使用RavenProfiler?,ravendb,nancy,Ravendb,Nancy,我正在使用NancyFX+RavenDB。我目前正在尝试实现Ayende推荐的技术,从AggregateRoot构建域引用。如果您阅读了该链接,您可以看到诀窍是加载父实例,然后使用RavenDB的“Include”语句预取引用的实例 我已经完成了所有这一切,并且它看起来很有效,但是我正在努力通过单元测试来确定引用的实例是否真的是预取的。下面是我的单元测试的一个片段来说明: [Fact] public void Should_preload_the_mentor_mentee_references
[Fact]
public void Should_preload_the_mentor_mentee_references_to_improve_performance()
{
//Given
var db = Fake.Db();
var mentor = Fake.Mentor(db);
var mentee = Fake.Mentee(db);
var relationship = Fake.Relationship(mentor, mentee, db);
//When
relationship = db
.Include("Mentor.Id")
.Include("Mentee.Id")
.Load<Relationship>(relationship.Id);
mentor = db.Load<User>(relationship.Mentor.Id);
mentee = db.Load<User>(relationship.Mentee.Id);
//Then
relationship.ShouldNotBe(null);
mentor.ShouldNotBe(null);
mentee.ShouldNotBe(null);
}
我还不得不添加对System.Web的引用,这让我不寒而栗。我认为这是行不通的
然后,我将适当的初始化添加到我的假db提供程序中,如下所示:
public class InMemoryRavenSessionProvider : IRavenSessionProvider
{
private static IDocumentStore documentStore;
public static IDocumentStore DocumentStore { get { return (documentStore ?? (documentStore = CreateDocumentStore())); } }
private static IDocumentStore CreateDocumentStore()
{
var store = new EmbeddableDocumentStore { RunInMemory = true};
store.Initialize();
store.Conventions.IdentityPartsSeparator = "-";
RavenProfiler.InitializeFor(store); //<-- Here is the Profiler line
return store;
}
public IDocumentSession GetSession()
{
return DocumentStore.OpenSession();
}
}
这不管用!它在RavenProfiler中失败,因为HttpContext为空-这就是我对System.Web的预感。哦,好吧
那么,如何计算向RavenDB实例发出的请求数呢?这可以在不需要MVC或System.Web的情况下完成吗?这样就可以轻松地将其固定到单元测试中
谢谢您可以为此使用以下代码:
session.Advanced.NumberOfRequests
RavenDB探查器不会针对嵌入式文档存储运行
另外,您还可以查看Raven source中的单元测试。最后一点注意:如果您使用这种技术,请记住,对db的每个调用都算作一个请求,因此在我上面的示例中,实际的请求数将是四个。前三个请求用于保存各种伪对象,最后一个单个请求是一次提取,将所有三个实例拉回来。好消息是,RavenDB中的预抓取工作正常:-)
var requests = RavenProfiler.CurrentRequestSessions();
session.Advanced.NumberOfRequests