使用的UnitOfWork模式犀牛模拟实体框架不工作 [英] Rhino Mock Entity Framework using UnitofWork Pattern not working

查看:233
本文介绍了使用的UnitOfWork模式犀牛模拟实体框架不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这样的第一次尝试,所以希望这是简单的。

This is my first attempt at something like this, so hopefully this is simple.

我创建了使用实体框架来访问数据库的WCF服务。我实现了一个的的UnitOfWork 的接口,所以我的服务可以使用EF同时仍然可测试

I have created a WCF service which uses Entity Framework to access the database. I have implemented a UnitOfWork interface so my service can use EF while still being testable.

下面是我的服务:

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

下面是我的UnitOfWork接口:

Here is my UnitOfWork interface:

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

当我使用本服务与 SQLUnitOfWork (使用EF)或用 InMemoryUnitOfWork (只在内存中的对象),那么它工作正常。

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.

在内存中的对象进行测试细跟我后,我尝试这个测试。

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



但是当我尝试使用它在犀牛模拟与Servers.Count的期望我收到以下错误:

But when I try using it in Rhino Mock with an Expectation on Servers.Count I get the following error:

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)

我在做什么错了?

推荐答案

MikeEast是正确的。 Rhino.Mocks没有做递归的嘲讽。您需要模拟了服务器属性返回的东西(只需要创建一个空的IObjectSet< tblServer>和设置了作为返回值)。

MikeEast is correct. Rhino.Mocks doesn't do recursive mocking. 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).

此外,你不吨要设置lambda表达式的期望。一旦一切都被编译,在代码中的lambda并在单元测试拉姆达是两个完全不同的方法(和你的期望总是会失败)。请参见 http://groups.google.com/group/rhinomocks/msg/318a35ae7536d90a了解更多信息。

Also, you don't want to set expectations on 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). See http://groups.google.com/group/rhinomocks/msg/318a35ae7536d90a for more details.

这篇关于使用的UnitOfWork模式犀牛模拟实体框架不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆