c# - Rhino mocks throws exception of "Callback arguments didn' t match the method arguments delegate"on the do 方法

标签 c# nhibernate testing delegates rhino-mocks

我正在使用 Rhino 模拟来更改 NHibernate DAL 的行为,以便在代码调用提交事务时,模拟框架会更改行为,从而回滚事务。我这样做的原因是为了集成测试,但我不想向数据库添加任何数据。

这是我正在测试的方法/类:

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;
        }
    }

我得到的错误是“回调参数与方法参数委托(delegate)不匹配”,它出现在 try、catch block 开始后的第二行。

任何人都可以帮助我了解此错误消息的含义以及我可以做些什么来解决这个问题吗?或者有人对如何使用 Nhibernate 进行集成测试有什么建议吗?

最佳答案

我没用过RhinoMocks,但是用过其他mock框架。我认为问题在于您的 Save 方法采用单个参数,但您提供给回调 Do 方法的委托(delegate)不采用参数。

那一行大概应该是这样的:

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

关于c# - Rhino mocks throws exception of "Callback arguments didn' t match the method arguments delegate"on the do 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819296/

相关文章:

c# - 循环 TabControl 中的控件

c# - 在 xamarin 中配置 Realm

c# - 从扩展它的类中获取类名

c# - Azure AD B2C 自定义策略,保护 API 连接器

c# - 按字段排序,该字段是 NHibernate/LINQ 中其他两个字段的组合/函数

ruby-on-rails - 打印 Rails 应用程序中所有测试的列表

unit-testing - 如何在 Kotlin 上测试数据类?

NHibernate 更改审计和组件

c# - nhibernate 有什么,那个 Entity Framework 4 不见了?

javascript - 在没有代码库的情况下测试 JavaScript