Jan 26, 2012 at 7:23 AM
Edited Jan 26, 2012 at 7:24 AM
Ok. So, convoluted as I am, I have found a way to get hold of the Container relevant to the test context so that I can do data prep with it. I like to do things the way it works in my head, unless I have been convinced otherwise. I haven’t
I’m sure this won’t carry your approval so much, Richard. :) I am equally sure there might be a much easier or better way of getting
the Container, but I haven’t discovered it.
Anyhow, if anybody else is interested in doing data prep
for your XAT, or other, tests, using your existing domain model, here is How I did it.
I added a new Repository called TestRepository.
It contains the following nested class and 1 method:
public class WrappedContainer
public object Container
public WrappedContainer GetContainer ()
var c = new WrappedContainer ();
// Wrap the Container.
c.Container = this.Container;
We return a WrappedContainer instead of an IDomainObjectContainer because the latter breaks the Naked Objects because of some internals when we use the XAT test constructs to get a hold of it.
In the SystemServices property of our XAT class, we then register a new TestRepository().
The last step to expose the Container to your test class is to add the following property on the class:
public IDomainObjectContainer Container
var objc = GetTestService (typeof (TestRepository)).GetAction ("Get Container").InvokeReturnObject ();
var wrapped = (TestRepository.WrappedContainer)objc.GetDomainObject ();
Just remember to wrap your test data prep code in:
If you need to execute stored procs, you can also add the following property:
/// Returns the underlying Object Context from the NakedObjects
/// Container for allowing access to stored procedures and functions
/// mapped in the model.
public ObjectContext Context
/* Copied from NakedObjects manual:
* 3.6.8. How to access the DB Context directly.
* The DB Context is accessible by casting the results of the Container.Instances<T>() method
* as an ObjectQuery. It is not important what type you make T (as long as it is one of your
* domain entity types) as the resulting context is not typed. */
return ((ObjectQuery)Container.Instances<Organisation> ()).Context;
And there you have it.
But to make all of this easier, I have created a XatBase class from where all my other XAT classes inherit.
I moved all my “Run Configuration” region code into the base (I over ride them in the specific XAT classes if I need something different).
I also moved the Container and Context properties, explained above, to the base class, as well as the Initialize and Cleanup methods, and made them virtual.
Now we can create utility methods that create entities for testing and returns them to the XAT tests as needed. The utility methods also register each created object into a List<T> on the base class. That list is then used during
cleanup to automatically delete the test data that was created.
In my case, everything I am testing hangs off an Organisation entity, therefore I only need to keep track of that. I have a stored procedure that does a cascading delete on the Organisation and all dependent records in the database based
on the OrganisationId. So it makes clean up really easy and robust.
And that’s all for now. :)