2011-05-06 12 views
9

मेरे पास NHibernate को स्थापित करने और फाड़ने के लिए सामान्य कोड है, जिसे मुझे अपने सभी परीक्षणों पर बहुत अधिक आवश्यकता है। क्या एक ही स्थान पर 'सभी परीक्षणों की आवश्यकता' कोड शामिल करने का कोई तरीका है, तो क्या यह सभी परीक्षणों पर लागू होता है? (Nunit के setup और teardown तरीकों की तरह यानी)मैं MSpec में प्रत्येक परीक्षण के साथ सेटअप और टियरडाउन कोड कैसे चला सकता हूं?

[Subject("Accessing the TAE allocation page")] 
public class when_a_request_to_the_tae_allocation_page_is_made 
{ 
    Establish context =() => NHTestHelper.StartTest(); //need for all tests 

    Because of =() => result = new AllocationController(true).Index(); 

    It should_display_the_page =() => result.ShouldBeAView(); 

    Cleanup nh =() => NHTestHelper.EndTest(); //need for all tests 

    static ActionResult result; 
} 

उत्तर

17

एक वर्ग IAssemblyContext इंटरफ़ेस का उपयोग कर लो। आपकी विनिर्देश कक्षाएं इस से प्राप्त नहीं होती हैं।

public class DataSpecificationBase : IAssemblyContext 
    { 
     public static Configuration configuration; 

     void IAssemblyContext.OnAssemblyComplete() 
     { 

      NHibernateSession.CloseAllSessions(); 
      NHibernateSession.Reset(); 

     } 

     void IAssemblyContext.OnAssemblyStart() 
     { 
      HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); 

      string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies(); 
      configuration = NHibernateSession.Init(new SimpleSessionStorage(), 
                mappingAssemblies, 
                new AutoPersistenceModelGenerator().Generate(), 
                "database.config"); 

      InitializeUserSession();    

      Console.WriteLine("OnAssemblyStart"); 
     } 

     void InitializeUserSession() 
     { 
      ITWEntityRepo entityRepo = new TWEntityRepo(); 
      // TWEntity entity = entityRepo.GetByUserName("1EB6472B-965B-41D5-8D77-3880D02FF518"); 
      TWEntity entity = entityRepo.GetByUserName("87BCA093-0B8C-4FDF-ABE8-1A843BA03608"); 

      UserSession.Instance().User = UserFactory.Create(entity); 
     } 
    } 
+0

बहुत बढ़िया, धन्यवाद! – Alistair

+0

अच्छा जेसन-बॉस! – TWright

+1

@Alistair: यह _each_ परीक्षण के लिए निष्पादित नहीं है, लेकिन सभी के लिए _once_। – Matthias

संबंधित मुद्दे