Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# gedw99 7/25/2007 11:38 PM (edited 7/26/2007 3:09 AM)
Has anyone managed to write a unit test domain boot strapper?

Something that tests from the Module from the Modules layer down. Dont want to do testing at the asp.net level as its too fragile.


Ged

# gedw99 7/26/2007 3:05 AM (edited 7/26/2007 3:08 AM)
Hey,

Got it working - Windsor makes you feel like a magician coder ; its a really well done.

But because i am NOT running under a HTTP Context my session manager for NHibernate blows up.
i had a look on the Castle site and not luck there so far.

TestCase 'Cuyahoga.Tests.DownloadModuleTests.Test01' failed: TestFixtureSetUp Failed
TestFixture failed: WebSessionStore: Could not obtain reference to HttpContext
at Castle.Facilities.NHibernateIntegration.Internal.WebSessionStore.ObtainSessionContext()
at Castle.Facilities.NHibernateIntegration.Internal.WebSessionStore.GetDictionary()
at Castle.Facilities.NHibernateIntegration.Internal.AbstractDictStackSessionStore.GetStackFor(String alias)
at Castle.Facilities.NHibernateIntegration.Internal.AbstractSessionStore.FindCompatibleSession(String alias)
at Castle.Facilities.NHibernateIntegration.Internal.DefaultSessionManager.OpenSession(String alias)
at Castle.Facilities.NHibernateIntegration.Internal.DefaultSessionManager.OpenSession()
at Cuyahoga.Core.DataAccess.CommonDao.GetObjectById(Type type, Int32 id) in C:\_Data\Projects\Cuyahoga\Code\\trunk\Core\DataAccess\CommonDao.cs:line 32



Anyone know how best to approach this problem ?



# martijnb 7/26/2007 10:01 AM
What are you trying to test here? It more looks like an integration test then a unit test, especially since you're saying 'Something that tests from the Module from the Modules layer down'.
# gedw99 7/26/2007 2:03 PM
Martin,


I am testing at the Module level, so i dont haveto worry about ASP.NET.
I call into the DB with a section ID, so it can get the exact module i need and test away.
I want to also write some integration tests at this level too.

From the Module down i am not hit by the HTTPCONTEXT not being there. Maybe a few things further down might need some mocking though.

Here the complete code. Its not refactored out at all so its easy to see whats happening.

The main problem is that the Casle NHibernate facility is forcing the creation of the Castle.Facilities.NHibernateIntegration.Internal.WebSessionStore.

I tried removing the httpHodule line but no luck.

Theres a forum post, but nothign conclusive.
http://forum.castleproject.org/viewtopic.php?t=1531

I feel this would be very useful for any Cuyahoga users.



using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using log4net;

using Cuyahoga.Core.Domain;
using Cuyahoga.Core.Service;
using Cuyahoga.Core.Service.SiteStructure;
using Cuyahoga.Core.Util;
using Cuyahoga.Tests.Util;
using Cuyahoga.Tests.Components;

using Cuyahoga.Modules.Downloads;



namespace Cuyahoga.Tests
{
[TestFixture]
public class DownloadModuleTests
{
//private static ILog log = LogManager.GetLogger(System.Reflection.Assembly.GetExecutingAssembly().GetType());
private static ILog log = LogManager.GetLogger(typeof(DownloadModuleTests));

private CuyahogaContainer _cuyahogaContainer;

private ISectionService _sectionService;
private ModuleLoader _moduleLoader;

DownloadsModule mod;

// Constr
public DownloadModuleTests()
{
// Plumb up base to load up all modules into the container
log4net.Config.XmlConfigurator.Configure();

// Createa Base Class for this
_cuyahogaContainer = new CuyahogaContainer();


this._sectionService = _cuyahogaContainer.Resolve(ISectionService);
this._moduleLoader = _cuyahogaContainer.Resolve(ModuleLoader);



// !!! It BOMBS OUT here with the exception above
int sectionId = 1;
string pathInfo = "pathinfo";
string content = null;


Section section = this._sectionService.GetSectionById(sectionId);
ModuleBase moduleBase = this._moduleLoader.GetModuleFromSection(section);
moduleBase.ModulePathInfo = pathInfo;

// Cast it
mod = (DownloadsModule)moduleBase;





}


[Test]
public void Test01()
{
Console.WriteLine("Hello world");

IList list = mod.GetAllFiles();
}
}
}
# gedw99 7/26/2007 2:23 PM
I have created a post on the Castle forums also, as this seems to be heavily related to Castle and Nhibernate facility.

http://forum.castleproject.org/viewtopic.php?t=3261

# martijnb 7/26/2007 4:57 PM
Could you remove the isWeb="true" attribute from facilities.config and see if that helps?

Btw, your test code really already is an integration test because the test involves multiple components. In a true unit test (for example, when testing DownloadsModule) you'd mock IFileService and ISessionManager.
# gedw99 7/26/2007 5:18 PM
Martin.

Removed the isWeb. That worked.
Yes i see what you mean about the fact that its an integration test.

I will write a DB boot strap utility next to allow me to clean and bootstrap the DB.
Might even just force the dropping of the DB and rebuilding.

i have heard about people using SQLLite for this, and using an in memory DB.
Speeds up testing dramatically.
Obviously you have to be careful because you are not using the same DB.
But a final test can be done using the real DB too.

anyway, thanks again for help.
I intend to wrapper some of the basic core and some modeules.
If you want i can send the unti tests to you and you could use a base for the trunk code.

GED
# gedw99 8/1/2007 6:43 PM
Got unit testing working. Or Integration testing as it were.

It woudl be much easier if the framework had a slight refactoring to make testing much simple.

1. Allow the boot strapping of the environment code to be outside the Web project. This is currently in the Web.Components an he Web.UI code.
Maybe using an interface modelled in the core and an implementation in the Web project and a different one in the Test Project.

Other than that it was not that difficult to get unit testing working.


G
# jmas 8/4/2007 12:17 AM
Martijn and GED,

I have been following this post with interest as I am currently trying to accomplish something similar.

I need to be able to use the same Core Cuyahoga Solution for two deployments. One using the standard Cuyahoga.Web as the default application. And the second solution using a regular Windows Console application as the startup app.

It is sort of what GED is doing by not using the UI Web layer and accessing the module layers. I don't even need the module layer, just the DAO layers on down to access the DB.

Does this seem easily possible?

I want a non-web solution to be able to take advantage of the DAO classes of the modules I have implemented. Since all the work is done for the Web Application it would be nice to be able to re-use the DAO layers in a re-packaged console app.

I want to deploy the Web solution with the Web GUI on an IIS server. And the non-web console solution as a backoffice application running on a different server. Both applications would access the same DB.

I assume the main changes are getting the CastleContainer and NHibernate facilities configured for the console application.

Is that the only challenge? Or, are there any major reasons why this might not be a good idea?


Thanks for any feedback,

Jmas



# martijnb 8/4/2007 9:57 AM
jmasMartijn and GED,
I want a non-web solution to be able to take advantage of the DAO classes of the modules I have implemented. Since all the work is done for the Web Application it would be nice to be able to re-use the DAO layers in a re-packaged console app.

I want to deploy the Web solution with the Web GUI on an IIS server. And the non-web console solution as a backoffice application running on a different server. Both applications would access the same DB.

I assume the main changes are getting the CastleContainer and NHibernate facilities configured for the console application.

Is that the only challenge? Or, are there any major reasons why this might not be a good idea?

It's perfectly possible to re-use the DAO and Service classes in a console app because they don't have a reference to the web project.

To setup the environment for the console app, make sure to copy the Castle configuration stuff intto the app.config, copy the castle .config files, and set isWeb to false in facilities.config. Then, you can initialize the container (probably the plain WindsorContainer will do) from your main() method in the console app and everything should work nicely.
If you need to resolve components from you console app, the static IoC helper class can be nice (https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/branches/1.5.x/Core/Util/IoC.cs). Initialize this one after creating the WindsorContainer.
# jmas 8/7/2007 2:43 PM
Thanks Martijn,

I am successfully re-using the Cuyahoga.Core and My.Module.Xyz in a prototype console application. Thanks for the suggestions.

Jmas


# foxiang 9/20/2007 3:42 AM
Thank you!
I saw the problem too.
Now ,I fixed it.
(a)