Home > MonoRail > Castle Monorail + Active Record + Testing + CreateSchemaFromFile

Castle Monorail + Active Record + Testing + CreateSchemaFromFile

5 December, 2006

It’s always a good idea to have your test database match your production database exactly to do that use:

ActiveRecordStarter.CreateSchemaFromFile()

Unlike CreateSchema which takes a best guess at how your schema should be CreateSchemaFromFile uses an SQL script which allows you to control exactly what the schema should be.

Call CreateSchemaFromFile as part of your PrepareSchema method in your AbstractTestCase class.

protected virtual void PrepareSchema()
{
// If you want to delete everything from the model.
// Remember to do it in a descendent dependency order
// Office.DeleteAll();
// User.DeleteAll();
// Another approach is to always recreate the schema
// (please use a separate test database if you want to do that)
ActiveRecordStarter.DropSchema();
ActiveRecordStarter.CreateSchemaFromFile("schema.sql");
}

Your schema.sql file should contain scripts in the standard MS SQL format:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Application_Status]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Application_Status]
GO

CREATE TABLE [dbo].[Application_Status] (
[ID] [int] NOT NULL ,
[Title] [varchar] (50) NOT NULL ,
[Description] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Advertisements
Categories: MonoRail
  1. 3 October, 2008 at 10:55 am

    Nice one, works well! Do you use any particular technique for initialising Active Record before running all of your tests (to prevent duplicate initialisation of Active Record)?

  2. 3 October, 2008 at 2:19 pm
  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: