简体   繁体   English

Rhino模拟在do方法上引发“回调参数与方法参数委托不匹配”的异常

[英]Rhino mocks throws exception of “Callback arguments didn't match the method arguments delegate” on the do method

I'm using Rhino mocks to change the behaviour of a NHibernate DAL so that when the commit transaction is called by the code the mock framework changes the behaviour so the transaction is rolled back. 我正在使用Rhino模拟来更改NHibernate DAL的行为,以便当代码调用提交事务时,模拟框架会更改行为,从而使事务回滚。 The reason i am doing this is that for integration testing but i don't want to add any data to the database. 我这样做的原因是为了进行集成测试,但我不想将任何数据添加到数据库中。

Here is my the method/class under test: 这是我正在测试的方法/类:

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

            }
        }
    }

}

This is the test code: 这是测试代码:

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

The error I am getting is "Callback arguments didn't match the method arguments delegate" which occurs on the 2nd line after the start of the try, catch block. 我收到的错误是“回调参数与方法参数委托不匹配”,它发生在try,catch块开始后的第二行。

Can anyone help me with the meaning of this error message and what i can do to resolve this? 谁能帮助我了解此错误消息的含义以及如何解决此问题? Or does anyone have an suggestions of how to carry out integration testing with Nhibernate? 还是有人对如何与Nhibernate进行集成测试提出建议?

Al

I haven't used RhinoMocks, but I have used other mock frameworks. 我没有使用RhinoMocks,但我已经使用了其他模拟框架。 I think the problem is that your Save method takes a single parameter, but the delegate that you've supplied to the callback Do method does not take an argument. 我认为问题在于您的Save方法采用单个参数,但是您提供给回调Do方法的委托没有采用参数。

That line should probably be like this: 该行可能应该是这样的:

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

Matt's answer is correct, but also consider using WhenCalled , instead of Do . 马特的答案是正确的,但也可以考虑使用WhenCalled代替, 待办事项 It's much easier to use, when you don't actually need to use the actual parameters passed in as in your case. 当您实际上不需要像实际情况一样使用传入的实际参数时,它更容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM