如何运行安装和拆卸code在MSpec每个测试? [英] How do I run setup and teardown code with each test in MSpec?

查看:178
本文介绍了如何运行安装和拆卸code在MSpec每个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通用的$ C $下建立和拆除NHibernate的,我需要在pretty的多少我所有的测试。有没有一种方法,包括了需要所有测试code在一个地方,然后把它应用到所有的测试? (即像NUnit的的设置拆卸方法)

  [主题(访问TAE分配页)
公共类when_a_request_to_the_tae_allocation_page_is_made
{
    建立上下文=()=> NHTestHelper.StartTest(); //需要所有测试

    由于=()的=>结果=新AllocationController(真).Index();

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

    清理NH =()=> NHTestHelper.EndTest(); //需要所有测试

    静态的ActionResult结果;
}
 

解决方案

必须使用IAssemblyContext接口的类。你的规范类不从这个继承。

 公共类DataSpecificationBase:IAssemblyContext
    {
        公共静态配置的配置;

        无效IAssemblyContext.OnAssemblyComplete()
        {

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

        }

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

            字符串[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
            配置= NHibernateSession.Init(新SimpleSessionStorage()
                                                   mappingAssemblies,
                                                   新AutoPersistenceModelGenerator()生成()
                                                   database.config);

            InitializeUserSession();

            Console.WriteLine(OnAssemblyStart);
        }

        无效InitializeUserSession()
        {
            ITWEntityRepo entityRepo =新TWEntityRepo();
            // TWEntity实体= entityRepo.GetByUserName(1EB6472B-965B-41D​​5-8D77-3880D02FF518);
            TWEntity实体= entityRepo.GetByUserName(87BCA093-0B8C-4FDF-ABE8-1A843BA03608);

            。UserSession.Instance()用户= UserFactory.Create(实体);
        }
    }
 

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)

[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;
}

解决方案

Have a class using the IAssemblyContext interface. Your specification classes do not inherit from this.

 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);
        }
    }

这篇关于如何运行安装和拆卸code在MSpec每个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆