如何配置Effort测试工具来模拟实体框架的DbContext与实际的SQL Server数据库启动并运行? [英] How would I configure Effort Testing Tool to mock Entity Framework's DbContext withOut the actual SQL Server Database up and running?

查看:124
本文介绍了如何配置Effort测试工具来模拟实体框架的DbContext与实际的SQL Server数据库启动并运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们团队的应用程序开发涉及使用Effort Testing Tool来模拟实体框架的DbContext。然而,似乎努力测试工具需要看到应用程序使用的实际SQL Server数据库,以模拟我们的实体框架的DbContext,这似乎违反了正确的单元测试原则。

Our team's application development involves using Effort Testing Tool to mock our Entity Framework's DbContext. However, it seems that Effort Testing Tool needs to be see the actual SQL Server Database that the application uses in order to mock our Entity Framework's DbContext which seems to going against proper Unit Testing principles.

原因是为了通过嘲笑与数据库连接相关的任何事情来单元测试我们的应用程序代码(例如实体框架的DbContext),我们不应该需要一个数据库才能启动并运行。

The reason being that in order to unit test our application code by mocking anything related to Database connectivity ( for example Entity Framework's DbContext), we should Never need a Database to be up and running.

如何配置Effort测试工具来模拟实体框架的DbContext,实际的SQL Server数据库是否正在运行?

How would I configure Effort Testing Tool to mock Entity Framework's DbContext withOut the actual SQL Server Database up and running?

*
更新:

* Update:

@ gert-arnold我们正在使用Entity Framework Model First实现后端模型和数据库。

@gert-arnold We are using Entity Framework Model First approach to implement the back-end model and database.

以下摘录来自测试代码:

The following excerpt is from the test code:

        connection = Effort.EntityConnectionFactory.CreateTransient("name=NorthwindModel");
        jsAudtMppngPrvdr = new BlahBlahAuditMappingProvider();
        fctry = new BlahBlahDataContext(jsAudtMppngPrvdr, connection, false);
        qryCtxt = new BlahBlahDataContext(connection, false);
        audtCtxt = new BlahBlahAuditContext(connection, false);
        mockedReptryCtxt = new BlahBlahDataContext(connection, false);
        _repository = fctry.CreateRepository<Account>(mockedReptryCtxt, null);
        _repositoryAccountRoleMaps = fctry.CreateRepository<AccountRoleMap>(null, _repository);

name = NorthwindModel属于我们的edmx文件,其中包含有关我们的数据库表的信息
和它们的对应关系。

The "name=NorthwindModel" pertains to our edmx file which contains information about our Database tables and their corresponding relationships.

如果我通过使连接像下面的代码行一样删除name = NorthwindModel,我收到一个错误,指出它期望参数:

If I remove the "name=NorthwindModel" by making the connection like the following line of code, I get an error stating that it expects an argument:

   connection = Effort.EntityConnectionFactory.CreateTransient(); // throws error

请问您可以解释上述代码应该如何重写?

Could you please explain how the aforementioned code should be rewritten?

推荐答案

您只需要连接字符串,因为Effort需要知道EDMX文件在哪里。

You only need that connection string because Effort needs to know where the EDMX file is.

EDMX文件包含创建与数据库中具有相同模式的内存存储所需的所有信息。您必须指定连接字符串,只因为我认为用户不必混淆EDMX路径会很方便。

The EDMX file contains all information required for creating an inmemory store with an identical schema you have in your database. You have to specify a connection string only because I thought it would be convenient if the user didn't have to mess with EDMX paths.

如果您检查CreateTransient方法你会看到它只是使用连接字符串来获取元数据的一部分。

If you check the implementation of the CreateTransient method you will see that it merely uses the connection string to get the metadata part of it.

public static EntityConnection CreateTransient(string entityConnectionString, IDataLoader dataLoader)
{
    var metadata = GetEffortCompatibleMetadataWorkspace(ref entityConnectionString);
    var connection = DbConnectionFactory.CreateTransient(dataLoader);
    return CreateEntityConnection(metadata, connection);
}

private static MetadataWorkspace GetEffortCompatibleMetadataWorkspace(ref string entityConnectionString)
{
    entityConnectionString = GetFullEntityConnectionString(entityConnectionString);

    var connectionStringBuilder = new EntityConnectionStringBuilder(entityConnectionString);

    return MetadataWorkspaceStore.GetMetadataWorkspace(
        connectionStringBuilder.Metadata,
        metadata => MetadataWorkspaceHelper.Rewrite(
            metadata, 
            EffortProviderConfiguration.ProviderInvariantName, 
            EffortProviderManifestTokens.Version1));
}

这篇关于如何配置Effort测试工具来模拟实体框架的DbContext与实际的SQL Server数据库启动并运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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