我正在使用Rhino mocks来更改NHibernate DAL的行为,以便当提交事务被代码调用时模拟框架改变了行为,所以事务被回滚。我这样做的原因是为了集成测试,但我不想向数据库添加任何数据。Rhino mocks抛出异常的“回调参数不匹配方法参数委托”的方法
这里是测试我的方法/类:
public class NHibernateDALSave<T> : IBaseDALSave<T> where T : class
{
protected ISession _session;
protected ISessionFactory _sessionFactory;
public NHibernateDALSave()
{
_sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public NHibernateDALSave(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
public void OpenSession()
{
if (_sessionFactory == null)
{
_sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
_session = _sessionFactory.OpenSession();
}
public virtual int Save(T objectToSave)
{
this.OpenSession();
using (_session)
{
using (ITransaction tx = _session.BeginTransaction())
{
try
{
Int32 NewId = Convert.ToInt32(_session.Save(objectToSave));
_session.Flush();
tx.Commit();
return NewId;
}
catch (Exception)
{
tx.Rollback();
throw;
}
}
}
}
}
这是测试代码:
public void SaveEmployee_Blank_Success()
{
//setup employee object to save
EmployeeDataContext employee = new EmployeeDataContext();
employee.Person = new PersonDataContext();
employee.PayRollNo = "12345";
employee.Person.Surname = "TEST";
//stub classes
ISessionFactory SessionFactoryStub = MockRepository.GenerateMock<ISessionFactory>();
ISession SessionStub = MockRepository.GenerateMock<ISession>();
ITransaction TranStub = MockRepository.GenerateMock<ITransaction>();
//Actual classes
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
ISession Session = sessionFactory.OpenSession();
ITransaction Tran = Session.BeginTransaction();
try
{
//Configure to prevent commits to the database
SessionStub.Stub(ss => ss.BeginTransaction()).Return(TranStub);
SessionStub.Stub(ss => ss.Save(Arg<EmployeeDataContext>.Is.Anything)).Do((Action)delegate { Session.Save(employee); });
SessionStub.Stub(ss => ss.Flush()).Do((Action)delegate { Session.Flush(); });
TranStub.Stub(ts => ts.Commit()).Do((Action)delegate { Tran.Rollback(); });
TranStub.Stub(ts => ts.Rollback()).Do((Action)delegate { Tran.Rollback(); });
SessionFactoryStub.Stub(sf => sf.OpenSession()).Return(SessionStub);
NHibernateDALSave<EmployeeDataContext> target = new NHibernateDALSave<EmployeeDataContext>(SessionFactoryStub);
target.Save(employee);
}
catch
{
Tran.Rollback();
throw;
}
}
我正的错误是“回调参数不匹配的方法参数委托“在try,catch块开始后出现在第二行。
任何人都可以帮助我解决这个错误信息的含义,我能做些什么来解决这个问题?或者有没有人对如何与Nhibernate进行集成测试提出建议?
铝