测试:实体框架通过伪造背景 [英] Testing: Entity Framework by faking context

查看:120
本文介绍了测试:实体框架通过伪造背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我吃了自己在这一刻。它像实体框架是不可测试。我读过很多,他们利用工作或MOQ或回购模式的单位职位和线程。

I am eating myself up at this moment. It is like Entity Framework isn't testable. I've read alot of posts and threads where they use unit of work or moq or the repo pattern.

我在,我不能改变我的我的应用程序的建筑风格,很多的阶段。该应用程序完全工作在这一刻,但可以肯定,我需要有一个高code覆盖,所以测试它。

I am in a phase that i can't change alot of my architechture of my application. The application fully works at this moment but to be sure I need to have a high code coverage, so testing it is.

有关测试,我使用的是'假的情况下的方法,我可以用假的一个嘲讽和实际一个用于连接到数据库。

For testing, I am using the 'fake context' method where I can use the fake one for mocking and the real one for connection to the database.

我用这个作为一个例子。
<一href=\"http://romiller.com/2010/09/07/ef-ctp4-tips-tricks-testing-with-fake-dbcontext/\">http://romiller.com/2010/09/07/ef-ctp4-tips-tricks-testing-with-fake-dbcontext/

I used this as an example. http://romiller.com/2010/09/07/ef-ctp4-tips-tricks-testing-with-fake-dbcontext/

有,可以看到上下文被分裂并用作一个接口。这样的:

There, you can see that the context is splitted and used as an interface. Like:

    public interface IEmployeeContext
    {
        IDbSet Department Departments { get; }
        IDbSet Employee Employees { get; }
        int SaveChanges();
    }

    public class EmployeeContext : DbContext, IEmployeeContext
    {
        public IDbSet Department Departments { get; set; }
        public IDbSet Employee Employees { get; set; }
    }

    public class FakeEmployeeContext : IEmployeeContext
    {
        public FakeEmployeeContext()
        {
            this.Departments = new FakeDepartmentSet();
            this.Employees = new FakeEmployeeSet();
        }

        public IDbSet Department Departments { get; private set; }

        public IDbSet Employee Employees { get; private set; }

        public int SaveChanges()
        { 
         return 0; 
        }
    }

    }

所以测试功能和一切正常。我似乎无法做的唯一一件事,就是与测试.STATE中有一个控制器,其中i检查是否是变了样:
EntityState.Modified

So tesing and everything works. The only thing i can't seem to do, is test a controller with .State in it where i check whether it's changed Like: EntityState.Modified

由于该使用接口,我需要补充的是在接口上下文。并创建一个新的。还是我失去了一些东西?很可能不是我在这方面创造了整个方法..我如何能设法得到这个工作的意向。

Because this uses an interface I need to add that into the interface context. And create a new one. Or am I missing something ? It is likely not the intention that I create the whole method in that context.. How can I manage to get this to work

推荐答案

你有没有考虑进行集成测试呢?

Have you considered performing integration tests instead?

您可以对一个真正的EF的DbContext集成测试,
只要给它的单元测试项目的App.config不同的连接字符串。

You can have integration tests against a real EF DBContext, just give it a different connection string in the App.config of the unit-tests project.

读<一个href=\"http://stackoverflow.com/questions/6904139/fake-dbcontext-of-entity-framework-4-1-to-test/6904479#6904479\">this和所有的答案吧。

这篇关于测试:实体框架通过伪造背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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