1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace !NAMESPACE
!.Tests
18 using System
.Configuration
;
20 using Castle
.ActiveRecord
;
21 using Castle
.ActiveRecord
.Framework
;
23 using NUnit
.Framework
;
26 /// This is a suggestion of base class that might
27 /// simplify the unit testing for the ActiveRecord
30 /// Basically you have to create a separate database
31 /// for your tests, which is always a good idea:
37 /// You have to decide if you want to administrate the
38 /// schema on the <c>test</c> database or let ActiveRecord
39 /// generate them for you during test execution. Check
40 /// <see cref="AbstractModelTestCase.PrepareSchema"/>
44 /// Note that this class enables lazy classes and collections
45 /// by using a <see cref="SessionScope"/>.
46 /// This have side effects. Some of your test must
47 /// invoke <see cref="FlushAndRecreateScope"/> sometimes
48 /// to persist the changes.
50 public abstract class AbstractModelTestCase
52 protected SessionScope scope
;
55 public virtual void FixtureInit()
61 public virtual void Init()
69 public virtual void Terminate()
77 public virtual void TerminateAll()
81 protected void FlushAndRecreateScope()
87 protected void CreateScope()
89 scope
= new SessionScope();
92 protected void DisposeScope()
98 /// If you want to delete everything from the model.
99 /// Remember to do it in a descendent dependency order
101 protected virtual void PrepareSchema()
103 // If you want to delete everything from the model.
104 // Remember to do it in a descendent dependency order
106 // Office.DeleteAll();
109 // Another approach is to always recreate the schema
110 // (please use a separate test database if you want to do that)
112 ActiveRecordStarter
.CreateSchema();
114 // You may also execute an external script
115 // to create your schema using ExecuteSchemaFromFile
117 // ActiveRecordStarter.ExecuteSchemaFromFile("script.sql");
120 protected virtual void DropSchema()
122 ActiveRecordStarter
.DropSchema();
125 protected virtual void InitFramework()
127 IConfigurationSource source
= ConfigurationSettings
.GetConfig("activerecord") as IConfigurationSource
;
129 ActiveRecordStarter
.Initialize( source
);
131 // Remember to add the types, for example
132 // ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post) );