Skip to content
A fake provider for System.Data.Common.DbConnection and related classes.
C#
Branch: master
Clone or download
jhgoodwin Merge pull request #8 from jhgoodwin/features/add_rudimentary_support…
…_dapper

Added basic impl FakeDbDataReader.GetFieldType and NextResult.
Latest commit 95da0c5 Aug 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
FakeDbProvider
.azure-devops-ci.yaml
.gitignore
CREDITS.txt
FakeDbProvider.sln
README.md

README.md

Fake Db Provider for System.Data.Common.DbConnection

Master Branch Status

  • Build Status

Sometimes mocking just isn't enough, so you need a realistic double. This is where fakes come in.

To get started, I copied the code from here: Entity Framework Test code in github

In order to make this work, I needed to fill in a few places. A proper fake should not throw new NotImplementedException for normal use of properties and methods.

How to use:

Create a new test class, inheriting from AbstractFakeDbTest, like this:

public class MyInsertTests: AbstractFakeDbTest
{
    private int _commitCount = 0;
    
    [Fact]
    public async void TransactionalSaveDataServer_SaveCausesOneSaveForSingleObject()
    {
        var upsertSaves = 0;
        var scalarExecutes = 0;
        ExecuteNonQueryAsync = (command, token) => Task.FromResult(upsertSaves++);
        ExecuteScalarAsync = (command, token) =>
        {
            scalarExecutes++;
            return Task.FromResult<object>("My stub scalar");
        };
        FakeConnection.TransactionChanged += OnTransactionChanged;
        try
        {
            var server = new MyClassUnderTest(Connection);
            var saveResult = await server.MySaveMethodAsync("My data to save");
            _commitCount.Should().Be(1);
            upsertSaves.Should().Be(1);
            scalarExecutes.Should().Be(1);
        }
        finally
        {
            FakeConnection.TransactionChanged -= OnTransactionChanged;
        }
    }
    
    protected virtual void OnTransactionChanged(object sender, TransactionChangedEventArgs e)
        => _commitCount = ((e.CurrentValue ?? e.NextValue) as FakeDbTransaction)?.CommitCount ?? 0;
}
You can’t perform that action at this time.