We've been testing our Sitecore code with the codeflood but wanted to do more to automate our tests on local and CI builds. I've been following the solution laid out by Mike Edwards on how to use NUNIT to run Sitecore tests -
Later, Dan Solovay had posted some thoughts on how to improve that -
So far this works great in a visual studio build. Config is copied from the Sitecore web site to the test project and NUNIT can execute tests that retrieve items from Sitecore, all without a context.
My problem - we make use of Glass Mapper for things like this:
Database database = global::Sitecore.Configuration.Factory.GetDatabase("master");
ISitecoreService SitecoreService = new SitecoreService(database);
var catalogItem = database.GetItem([guid to our item]);
Assert.IsNotNull(catalogItem);
var catalog = SitecoreService.CreateType<ProductCatalog>(catalogItem, true, true);
Assert.NotNull(catalog);
Assert other things on our ProductCatalog class...
The problem seems to be that Glass Mapper's SitecoreService constructor needs a context and if it doesn't get one, it uses "Default". Since we're executing in NUNIT, there isn't a context and the creation of Sitecore Service fails.
I doubt there is a clear cut answer that fixes this but I'd be interested in anyone's thoughts.
Maybe the use of Glass Mapper in the test just isn't possible without the Sitecore context. On the other hand, I am by no means a Glass expert - maybe there is a different way to go about mapping my class in the test?