Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run setup and teardown code with each test in MSpec?

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;
}
like image 202
Alistair Avatar asked May 06 '11 02:05

Alistair


People also ask

Does tearDown run after every test?

Code in a teardown() block is run upon completion of a test file, even if it exits with an error.

What are setUp () and tearDown () methods in mobile app testing?

setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.

What is the use of setUp () and tearDown ()?

When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.


1 Answers

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);
        }
    }
like image 173
Jason Watts Avatar answered Oct 24 '22 18:10

Jason Watts