RavenDB Connecting to the built-in document repository during Unit Test

Edit

It turns out that I am storing the document correctly, so the initial part of this question is incorrect, possibly due to my inexperience with RavenDB. However, I still have a question about the possibility of opening RavenDB Management Studio when using the EmbeddableDocumentStore in the unit test.


It seems I have a problem storing documents in an EmbeddableDocumentStore during unit test using NUnit. To check if I am actually storing a document, I am trying to connect to the embedded database.

When I try to open the url http: // computer_name: 8080 / (for some reason raven db always uses the computer name on my computer), the browser loading bar just rotates, and if I stop the unit test and try the URL again. Chrome just gives me a message that it cannot connect. Here is some code for the context.

[TestFixture] public class Test2 : IDisposable { private EmbeddableDocumentStore _documentStore; [SetUp] public void SetUp() { if (_documentStore != null) return; _documentStore = new EmbeddableDocumentStore { DataDirectory = "Data", RunInMemory = true, UseEmbeddedHttpServer = true }; _documentStore.Configuration.AnonymousUserAccessMode = AnonymousUserAccessMode.All; _documentStore.Initialize(); } [Test] public void Test() { var document = StaticData.getdocument(); using (var session = _documentStore.OpenSession()) { session.Store(document); session.SaveChanges(); } using (var session = _documentStore.OpenSession()) { var test = session.Load<ObjectType>().Length; //This is always 0 } } public void Dispose() { _documentStore.Dispose(); } } 

I also have a Raven.Studio.xap file in the root of my test project.

I use VS2012 and .Net 4.5, if that matters.

+6
source share
2 answers

Here you go:

 static public void WaitForUserToContinueTheTest(EmbeddableDocumentStore documentStore, bool debug = true) { if (debug && Debugger.IsAttached == false) return; documentStore.SetStudioConfigToAllowSingleDb(); documentStore.DatabaseCommands.Put("Pls Delete Me", null, RavenJObject.FromObject(new { StackTrace = new StackTrace(true) }), new RavenJObject()); documentStore.Configuration.AnonymousUserAccessMode = AnonymousUserAccessMode.All; using (var server = new HttpServer(documentStore.Configuration, documentStore.DocumentDatabase)) { server.StartListening(); Process.Start(documentStore.Configuration.ServerUrl); // start the server do { Thread.Sleep(100); } while (documentStore.DatabaseCommands.Get("Pls Delete Me") != null && (debug == false || Debugger.IsAttached)); } } 
+4
source

The exact details on how to apply the @Ayende Rahien solution are not in his answer, so I will talk about specific steps here:

  • If you create your EmbeddableDocumentStore with UseEmbeddedHttpServer set to true, delete it since WaitForUserToContinueTheTest creates a server for you.

  • In the test code that you want to debug, call the WaitForUserToContinueTheTest method.

  • Set a breakpoint in Visual Studio after the WaitForUserToContinueTheTest method.

  • When launched in debug mode, a browser window opens with the localhost URL: 8080 / raven / studio.html or: 8080 / raven / studio.html.

  • To exit the while loop in WaitForUserToContinueTheTest, use the Raven HTTP GUI and go to “Documents” → “All Documents”. On the right side, right-click the line "Pls delete me" and click "Delete." Visual Studio should now continue the test.

(By the way, I hope the RavenDB team improves the unit testing documentation.)

0
source

Source: https://habr.com/ru/post/927566/


All Articles