Sorry, I was trying to keep code to the minimum.
The test fixture is derived from RepositoryUnitTestsBase, which calls the
Init method on NHibernateSession object.
[TestFixture]
public class FlierRepositoryTest : RepositoryUnitTestsBase {
private readonly IRepository<Flier> _flierRepository = new
Repository<Flier>();
[Test]
public void TestCanLoadFlier() {
Flier flierFromDb = _flierRepository.Get(1);
Assert.AreEqual("101", flierFromDb.StockKeepingUnit.TrimEnd());
Assert.AreEqual("Lighting", flierFromDb.Description);
Assert.AreEqual(50000, flierFromDb.QuantityOnHand);
}
}
public static class NHibernateSession {
public static Configuration Init(ISessionStorage storage, string[]
mappingAssemblies) {
return Init(storage, mappingAssemblies, null, null);
}
public static Configuration Init(ISessionStorage storage, string[]
mappingAssemblies, string cfgFile) {
return Init(storage, mappingAssemblies, cfgFile, null);
}
public static Configuration Init(ISessionStorage storage, string[]
mappingAssemblies, string cfgFile, string validatorCfgFile) {
Check.Require(storage != null, "storage mechanism was null but
must be provided");
Configuration cfg = ConfigureNHibernate(cfgFile);
ConfigureNHibernateValidator(cfg, validatorCfgFile);
AddMappingAssembliesTo(cfg, mappingAssemblies);
SessionFactory = cfg.BuildSessionFactory();
Storage = storage;
return cfg;
}.
public abstract class RepositoryUnitTestsBase {
[SetUp]
public void SetUp() {
string nhibernateConfig =
ConfigurationSettings.AppSettings["nhibernate.config.path"];
string mappingAssemblies =
ConfigurationSettings.AppSettings["nhibernate.mapping.assembly"];
Check.Require(!string.IsNullOrEmpty(nhibernateConfig),
"Please add an AppSetting to your app.config for
'nhibernate.config.path.' This setting " +
"should be a relative path to the location of the
configuration file containing NHibernate config options.");
Check.Require(!string.IsNullOrEmpty(mappingAssemblies),
"Please add an AppSetting to your app.config for
'nhibernate.mapping.assembly.' This setting " +
"takes a comma delimited list of assemblies containing
NHibernate mapping files. Including '.dll' " +
"at the end of each is optional.");
NHibernateSession.Init(new SimpleSessionStorage(),
mappingAssemblies.Split(','), nhibernateConfig);
NHibernateSession.Current.BeginTransaction();
}
}
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Jeff Brown
Sent: Tuesday, November 25, 2008 1:46 PM
To: [email protected]
Subject: MbUnit Re: Error in MbUnit testing?
How does NHibernateSession.Storage get initialized?
_____
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Lars Zeb
Sent: Tuesday, November 25, 2008 1:05 PM
To: [email protected]
Subject: MbUnit Error in MbUnit testing?
I am just beginning to use the Sharp Architecture. I wrote a test to
exercise a repository. The actual test method is:
public void TestCanLoadFlier() {
Flier flierFromDb = _flierRepository.Get(1);
Assert.AreEqual("101", flierFromDb.StockKeepingUnit.TrimEnd());
Assert.AreEqual("Lighting", flierFromDb.Description);
Assert.AreEqual(50000, flierFromDb.QuantityOnHand);
}
The test blows on a null object reference trying to get a Session object
from NHibernate. However, if I swap out MbUnit and use NUnit instead, the
test succeeds. What could I possibly be doing wrong?
The code below is from Sharp, to which I've referenced in the above code.
Thanks, Lars
public class Repository<T> : RepositoryWithTypedId<T, int>,
INHibernateRepository<T> { }
public class RepositoryWithTypedId<T, IdT> :
INHibernateRepositoryWithTypedId<T, IdT>
{
protected ISession Session {
get { return NHibernateSession.Current; }
}
public T Get(IdT id) {
return Session.Get<T>(id);
}
}
public static class NHibernateSession
{
...
public static ISessionStorage Storage { get;
set; }
public static ISession Current {
get {
ISession session =
Storage.Session; // ****This is NHibernateSession.cs:line 88****
if (session == null) {
session =
SessionFactory.OpenSession();
Storage.Session = session;
}
return session;
}
}
Type:System.NullReferenceException
Message:Object reference not set to an instance of an object.
Source:SharpArch.Data
TargetSite:NHibernate.ISession get_Current()
HelpLink:null
StackTrace:
at SharpArch.Data.NHibernate.NHibernateSession.get_Current() in
C:\MyStuff\Dev\Projects\SharpArchitecture\src\SharpArch\SharpArch.Data\NHibe
rnate\NHibernateSession.cs:line 88
at SharpArch.Data.NHibernate.RepositoryWithTypedId`2.Get(IdT id) in
C:\MyStuff\Dev\Projects\SharpArchitecture\src\SharpArch\SharpArch.Data\NHibe
rnate\Repository.cs:line 39
<BR
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MbUnit.User" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/MbUnitUser?hl=en
-~----------~----~----~----~------~----~------~--~---