sql-serverentity-frameworkunit-testingmockingeffort

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


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.

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.

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

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

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?


Solution

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

    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.

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