2013-04-09 48 views
1

我通常嘲笑IDpecmentSession在我的MSpec与Machine.Fakes,RavenDB家伙不喜欢。如何在Machine.Fakes中使用EmbeddableDocumentStore?使用Machine.Fakes单元测试与RavenDB EmbeddableDocumentStore

+0

嗨Jason。整洁的东西,但请记住,StackOverflow是关于提问的问题。我有很多我和别人分享的很酷的代码,但是有更合适的地方。您可以考虑[RavenDB Google Group](https://groups.google.com/forum/?fromgroups#!forum/ravendb),博客文章,GitHub上的项目,或将其贡献给[RavenDB.Contrib] (https://github.com/ravendb/ravendb.contrib)项目。 – 2013-04-09 15:25:21

+0

我试图做一个社区维基。我如何设置? – 2013-04-09 19:08:14

+0

from http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ - “底线 - 不要犹豫,问任何问题并回答你自己的问题堆栈交换站点“。 ??? – 2013-04-09 19:09:47

回答

2

要点: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(); 
} 

的服务我测试

public interface 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; 
    } 
}