I have generic code for setting up and tearing down NHibernate, which I need on pretty much all my tests. Is there a way to include the 'need for all tests' code in one place, then have it applied to all tests? (ie like Nunit's setup
and teardown
methods)
我有用于设置和拆除NHibernate的通用代码,我几乎所有的测试都需要它。有没有办法在一个地方包含“需要所有测试”代码,然后将它应用于所有测试? (比如Nunit的设置和拆卸方法)
[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;
}
1 个解决方案
#1
17
Have a class using the IAssemblyContext interface. Your specification classes do not inherit from this.
使用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);
}
}
#1
17
Have a class using the IAssemblyContext interface. Your specification classes do not inherit from this.
使用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);
}
}