Applied patch from Jan Limpens 'ReflectionBasedDictionaryAdapter needs to check if...
[castle.git] / Tools / VSNetWizards / CastleTemplates / VS8 / CSharp / ARProjectTest / AbstractModelTestCase.cs
blob85f0b1d88fcd0c527c60016382af647829a05974
1 namespace !NAMESPACE!
3 using System;
5 using Castle.ActiveRecord;
6 using Castle.ActiveRecord.Framework;
7 using Castle.ActiveRecord.Framework.Config;
9 using NUnit.Framework;
11 /// <summary>
12 /// This is a suggestion of base class that might
13 /// simplify the unit testing for the ActiveRecord
14 /// classes.
15 /// <para>
16 /// Basically you have to create a separate database
17 /// for your tests, which is always a good idea:
18 /// - production
19 /// - development
20 /// - test
21 /// </para>
22 /// <para>
23 /// You have to decide if you want to administrate the
24 /// schema on the <c>test</c> database or let ActiveRecord
25 /// generate them for you during test execution. Check
26 /// <see cref="AbstractModelTestCase.PrepareSchema"/>
27 /// </para>
28 /// </summary>
29 /// <remarks>
30 /// Note that this class enables lazy classes and collections
31 /// by using a <see cref="SessionScope"/>.
32 /// This have side effects. Some of your test must
33 /// invoke <see cref="Flush"/>
34 /// to persist the changes.
35 /// </remarks>
36 public abstract class AbstractModelTestCase
38 protected SessionScope scope;
40 [TestFixtureSetUp]
41 public virtual void FixtureInit()
43 InitFramework();
46 [SetUp]
47 public virtual void Init()
49 PrepareSchema();
51 CreateScope();
54 [TearDown]
55 public virtual void Terminate()
57 DisposeScope();
59 DropSchema();
62 [TestFixtureTearDown]
63 public virtual void TerminateAll()
67 protected void Flush()
69 SessionScope.Current.Flush();
72 protected void CreateScope()
74 scope = new SessionScope(FlushAction.Never);
77 protected void DisposeScope()
79 scope.Dispose();
82 /// <summary>
83 /// If you want to delete everything from the model.
84 /// Remember to do it in a descendent dependency order
85 /// </summary>
86 protected virtual void PrepareSchema()
88 // If you want to delete everything from the model.
89 // Remember to do it in a descendent dependency order
91 // Office.DeleteAll();
92 // User.DeleteAll();
94 // Another approach is to always recreate the schema
95 // (please use a separate test database if you want to do that)
97 ActiveRecordStarter.CreateSchema();
100 protected virtual void DropSchema()
102 ActiveRecordStarter.DropSchema();
105 protected virtual void InitFramework()
107 IConfigurationSource source = ActiveRecordSectionHandler.Instance;
109 ActiveRecordStarter.Initialize( source );
111 // Remember to add the types, for example
112 // ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post) );
114 // Or to use the assembly that holds the ActiveRecord types
115 // ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("MyARProject"), source);