简体   繁体   English

使用UnitofWork Pattern的Rhino Mock Entity Framework无法正常工作

[英]Rhino Mock Entity Framework using UnitofWork Pattern not working

This is my first attempt at something like this, so hopefully this is simple. 这是我第一次尝试这样的事情,所以希望这很简单。

I have created a WCF service which uses Entity Framework to access the database. 我创建了一个WCF服务,它使用Entity Framework来访问数据库。 I have implemented a UnitOfWork interface so my service can use EF while still being testable. 我已经实现了UnitOfWork接口,因此我的服务可以使用EF,同时仍然可以测试。

Here is my service: 这是我的服务:

public class ProjectService : IProjectService
{
    private IUnitOfWork db = null;

    public ProjectService(IUnitOfWork unitofwork)
    {
        db = unitofwork;
    }

    public int RegisterSite(int CPUID)
    {
        if (db.Servers.Count(x => x.CPUID == CPUID) > 0)
        {
            // do something
        }

        return 0;
    }
}

Here is my UnitOfWork interface: 这是我的UnitOfWork界面:

public interface IUnitOfWork
{
    IObjectSet<tblClient> Clients { get; }
    IObjectSet<tblServer> Servers { get; }
    IObjectSet<tblSite> Sites { get; }
    IObjectSet<tblServerLog> ServerLogs { get; }
    void Commit();
}

When I use this Service with either concrete implementations of a SQLUnitOfWork (using EF) or with a InMemoryUnitOfWork (just in memory objects) then it works fine. 当我将此服务与SQLUnitOfWork (使用EF)或InMemoryUnitOfWork (仅在内存对象中)的具体实现SQLUnitOfWork使用时,它可以正常工作。

After testing fine with my in memory objects i tried this test. 在我的内存对象测试完成后,我尝试了这个测试。

[Test]
public void RegisterAnExistingServer()
    {
        MockRepository mocks = new MockRepository();

        IUnitOfWork MockUnitOfWork = mocks.DynamicMock<IUnitOfWork>();

        ProjectService service = new ProjectService(MockUnitOfWork);


        Expect.Call(MockUnitOfWork.Servers.Count(x => x.CPUID == 1234)).Return(0);

        mocks.ReplayAll();

        int NewSiteID = service.RegisterSite(1234);

        mocks.VerifyAll();
    }

But when I try using it in Rhino Mock with an Expectation on Servers.Count I get the following error: 但是,当我尝试在Rhino Mock中使用它与Servers.Count上的期望时,我收到以下错误:

System.ArgumentNullException : Value cannot be null.
Parameter name: arguments
at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)
at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ref ReadOnlyCollection`1 arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Linq.Queryable.Count(IQueryable`1 source, Expression`1 predicate)

What am I doing wrong?? 我究竟做错了什么??

MikeEast is correct. MikeEast是对的。 Rhino.Mocks doesn't do recursive mocking. Rhino.Mocks不做递归模拟。 You need to mock up the Servers property to return something (just create an empty IObjectSet<tblServer> and set that up as the return value). 您需要模拟Servers属性以返回一些内容(只需创建一个空的IObjectSet <tblServer>并将其设置为返回值)。

Also, you don't want to set expectations on lambdas. 此外,你不想对lambdas设定期望。 Once everything gets compiled, the lambda in your code and the lambda in your unit test are two totally different methods (and your expectation will always fail). 一旦编译完所有内容,代码中的lambda和单元测试中的lambda就是两种完全不同的方法(并且你的期望总是会失败)。 See http://groups.google.com/group/rhinomocks/msg/318a35ae7536d90a for more details. 有关详细信息,请参阅http://groups.google.com/group/rhinomocks/msg/318a35ae7536d90a

There is probably some reflection going on internally and hence you are not getting a straight forward call to your unit of work. 内部可能会有一些反思,因此您无法直接致电您的工作单位。

I strongly suggest switching to nhibernate. 我强烈建议切换到nhibernate。 Also, ditch WCF. 还有,沟渠WCF。

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

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