2011-01-27 65 views
1

我正在使用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進行集成測試提出建議?

回答

5

馬特的答案是正確的,但也可以考慮使用WhenCalled,而不是Do。當你實際上不需要使用傳入的實際參數時,使用起來會更容易。

5

我沒有使用RhinoMocks,但我已經使用其他模擬框架。我認爲問題在於你的Save方法只有一個參數,但是你提供給回調Do方法的委託並不帶參數。

該行也許應該是這樣的:

SessionStub.Stub(ss => ss.Save(Arg<EmployeeDataContext>.Is.Anything)).Do(arg => Session.Save(employee))