1
저는 보통 RavenDB 사람들이 좋아하지 않는 Machine.Fakes를 사용하여 MSpec에서 IDocumentSession을 모의합니다. Machine.Fakes와 함께 EmbeddableDocumentStore를 어떻게 사용합니까?RavenDB를 이용한 Machine.Fakes 유닛 테스팅 사용하기 EmbeddableDocumentStore
저는 보통 RavenDB 사람들이 좋아하지 않는 Machine.Fakes를 사용하여 MSpec에서 IDocumentSession을 모의합니다. Machine.Fakes와 함께 EmbeddableDocumentStore를 어떻게 사용합니까?RavenDB를 이용한 Machine.Fakes 유닛 테스팅 사용하기 EmbeddableDocumentStore
요점 : https://gist.github.com/JasonMore/5345697
후킹 RavenDB InMemory Machine.Fakes
public class RavenInMemorySlowRunner
{
public class NoStaleQueriesAllowed : IDocumentQueryListener
{
public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
{
queryCustomization.WaitForNonStaleResults();
}
}
public class AllDocumentsById : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinition
{
Name = "AllDocumentsById",
Map = "from doc in docs let DocId = doc[\"@metadata\"][\"@id\"] select new {DocId};"
};
}
}
public static EmbeddableDocumentStore Store { get; set; }
public static IDocumentSession Session { get; set; }
OnEstablish context = fakeAccessor =>
{
fakeAccessor.Configure(x => x.For<IDocumentSession>().Use(() =>
{
if (Store == null)
{
Store = new EmbeddableDocumentStore { RunInMemory = true };
Store.RegisterListener(new NoStaleQueriesAllowed());
Store.Initialize();
// RegisterServicesWithNinject is in the project where the indexes live
IndexCreation.CreateIndexes(typeof(RegisterServicesWithNinject).Assembly, Store);
IndexCreation.CreateIndexes(typeof(RavenInMemorySlowRunner).Assembly, Store);
}
if (Session == null)
{
Session = Store.OpenSession();
}
return Session;
}));
};
OnCleanup subject = subject =>
{
Session.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex("AllDocumentsById", new IndexQuery());
Session.SaveChanges();
Session.Dispose();
};
}
테스트
public class CurrentSiteModelServiceSpecs : WithSubject<CurrentSiteModelService>
{
Establish that =() =>
{
// use the raven in memory runner since
// we are using lots of raven magic in this service
With(new RavenInMemorySlowRunner());
};
}
/// <summary>
/// Determine Site Model for Dev
/// </summary>
public class When_Determining_SiteModel_for_dev : CurrentSiteModelServiceSpecs
{
public static SiteViewModel _siteViewModelResult;
public static IHttpCookie _cookie;
Because of =() =>
_siteViewModelResult = Subject.DetermineSiteModelForDevEnvironment();
}
public class And_Cookie_not_set : When_Determining_SiteModel_for_dev
{
It returns_null =() =>
_siteViewModelResult.ShouldBeNull();
}
public class And_Cookie_set : When_Determining_SiteModel_for_dev
{
Establish that =() =>
{
_cookie = An<IHttpCookie>();
_cookie.Value = "site/123";
The<ICookieService>()
.WhenToldTo(x => x.GetCookie(".CMS3DevSite"))
.Return(_cookie);
var site1 = new SiteModel{ Id = "site/123", HostName = "foo" };
var site2 = new SiteModel{ Id = "site/456", HostName = "bar" };
The<IDocumentSession>().Store(site1);
The<IDocumentSession>().Store(site2);
The<IDocumentSession>().SaveChanges();
};
It loads_site =() =>
_siteViewModelResult.HostName.ShouldEqual("foo");
}
public class And_Cookie_set_but_site_does_not_exist : When_Determining_SiteModel_for_dev
{
Establish that =() =>
{
_cookie = An<IHttpCookie>();
_cookie.Value = "site/123";
The<ICookieService>()
.WhenToldTo(x => x.GetCookie(".CMS3DevSite"))
.Return(_cookie);
};
It returns_null =() =>
_siteViewModelResult.ShouldBeNull();
}
서비스에 내가
0,123,516을 테스트하고 있습니다공용 인터페이스 ICurrentSiteModelService { RedirectToResult SetSiteModel (string path, Uri url); }
public class CurrentSiteModelService : ICurrentSiteModelService
{
readonly IDocumentSession _documentSession;
readonly ICookieService _cookieService;
public CurrentSiteModelService(
IDocumentSession documentSession,
ICookieService cookieService)
{
_documentSession = documentSession;
_cookieService = cookieService;
}
// cruft removed here
// load site in dev mode based on cookie.
internal SiteViewModel DetermineSiteModelForDevEnvironment()
{
var cookie = _cookieService.GetCookie(".CMS3DevSite");
if (cookie != null && !String.IsNullOrEmpty(cookie.Value))
{
SiteViewModel site = _documentSession.
Query<SiteViewModel, SiteViewIndex>()
.Where(s => s.Id == cookie.Value)
.AsProjection<SiteViewModel>()
.FirstOrDefault();
if (site != null)
{
return site;
}
}
return null;
}
}
안녕 Jason. 깔끔한 것들,하지만 StackOverflow는 질문에 대해 염두에 두십시오. 나는 다른 사람들과 공유 할 멋진 코드를 많이 가지고 있지만 더 적절한 장소가 있습니다. [RavenDB Google 그룹] (https://groups.google.com/forum/?fromgroups#!forum/ravendb), 블로그 게시물, GitHub의 프로젝트 또는 [RavenDB.Contrib] (https://github.com/ravendb/ravendb.contrib) 프로젝트. –
커뮤니티 위키를 만들려고했습니다. 어떻게 설정해야합니까? –
(http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) - "최종 결과 - 어떤 질문에 대해서도 자신의 질문을 물어보고 주저하지 마십시오. 스택 Exchange 사이트. " ??? –