Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Last active August 29, 2015 14:14
Show Gist options
  • Save PureKrome/ba3a519d807e5103243c to your computer and use it in GitHub Desktop.
Save PureKrome/ba3a519d807e5103243c to your computer and use it in GitHub Desktop.
Why is TinyIoC is disposing of this instance?
/*
install-package raven.database
install-package xunit
install-package nancy.testing
install-package shouldly
install-package nbuilder
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsQuery.Utility;
using FizzWare.NBuilder;
using Nancy;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Shouldly;
using Xunit;
namespace ConsoleApplication4
{
public class SampleModule : NancyModule
{
private readonly IDocumentStore _documentStore;
public SampleModule(IDocumentStore documentStore) : base("/")
{
_documentStore = documentStore;
Get["/"] = _ => "Hello World";
}
}
public class SampleRepo
{
[Fact]
public async Task DoTest()
{
// Arrange.
var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
Conventions = new DocumentConvention
{
DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite
}
};
documentStore.Initialize();
using (var session = documentStore.OpenAsyncSession())
{
foreach (var item in Builder<FakeData>.CreateListOfSize(1000).Build())
{
await session.StoreAsync(item);
}
await session.SaveChangesAsync();
}
var browser = new Nancy.Testing.Browser(with =>
{
with.Module<SampleModule>();
with.Dependency<IDocumentStore>(documentStore); // EXCEPTION
//with.Dependency(documentStore); // WORKS
});
using (var session2 = documentStore.OpenAsyncSession())
{
var foo = await session2.LoadAsync<FakeData>("fakeDatas/1");
}
// Act.
var result = browser.Get("/");
// Assert.
result.StatusCode.ShouldBe(HttpStatusCode.OK);
}
}
public class FakeData
{
public string Id { get; set; }
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment