Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented API for set dbconnection #111

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;
using rgvlee.Core.Common.Helpers;
using System.Data;
using System.Data.Common;

namespace EntityFrameworkCore.Testing.Common.Helpers
{
Expand All @@ -19,6 +21,21 @@ public abstract class BaseMockedDbContextBuilder<TDbContext> : IMockedDbContextB
/// </summary>
public abstract TDbContext MockedDbContext { get; }


/// <summary>
/// The parameter that will be used in direct commands (context.Database.GetDbConnection();).
/// </summary>
/// <param name="dbConnection">
/// db connection instance.
/// </param>
/// <returns>The mocked db context builder.</returns>
public IMockedDbContextBuilder<TDbContext> UseDbConnection(DbConnection dbConnection)
{
EnsureArgument.IsNotNull(dbConnection, nameof(dbConnection));
Options.DbConnection = dbConnection;
return this;
}

/// <summary>
/// The parameters that will be used to create the mocked db context and, if one is not provided,
/// the in-memory context that the mocked db context will use for in-memory provider supported operations.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -30,6 +31,11 @@ public abstract class BaseMockedDbContextFactory<TDbContext> where TDbContext :
/// </summary>
protected readonly TDbContext DbContext;

/// <summary>
/// The db connection instance that the mocked db context will use in direct commands.
/// </summary>
protected readonly DbConnection DbConnection;

/// <summary>
/// Constructor.
/// </summary>
Expand Down Expand Up @@ -69,6 +75,11 @@ protected BaseMockedDbContextFactory(MockedDbContextFactoryOptions<TDbContext> o
{
DbContext = (TDbContext) Activator.CreateInstance(typeof(TDbContext), ConstructorParameters?.ToArray());
}

if (options.DbConnection != null)
{
DbConnection = options.DbConnection;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;

namespace EntityFrameworkCore.Testing.Common.Helpers
Expand All @@ -19,5 +21,10 @@ public class MockedDbContextFactoryOptions<TDbContext> where TDbContext : DbCont
/// the in-memory context that the mocked db context will use for in-memory provider supported operations.
/// </summary>
public IEnumerable<object> ConstructorParameters { get; set; }

/// <summary>
/// The db connection will be use in direct commands (context.Database.GetDbConnection();).
/// </summary>
public DbConnection DbConnection { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/EntityFrameworkCore.Testing.Moq.Tests/FeatureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Data.Common;
using EntityFrameworkCore.Testing.Common.Tests;
using EntityFrameworkCore.Testing.Moq.Helpers;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;

namespace EntityFrameworkCore.Testing.Moq.Tests
{
public class FeatureTests : BaseForTests
{
[Test]
public void Set_DbConnection()
{
var mockConnection = new Mock<DbConnection>();
var mockedDbContext = new MockedDbContextBuilder<TestDbContext>().UseDbConnection(mockConnection.Object).MockedDbContext;
var connection = mockedDbContext.Database.GetDbConnection();
Assert.That(connection, Is.EqualTo(mockConnection.Object));
}

[Test]
public void Default_DbConnection()
{
var mockedDbContext = new MockedDbContextBuilder<TestDbContext>().MockedDbContext;
var connection = mockedDbContext.Database.GetDbConnection();
Assert.IsNull(connection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public override TDbContext Create()

var relationalConnectionMock = new Mock<IRelationalConnection>();
relationalConnectionMock.Setup(x => x.CommandTimeout).Returns(() => 0);

relationalConnectionMock.Setup(x => x.DbConnection).Returns(() => DbConnection);
var relationalConnection = relationalConnectionMock.Object;

var dependenciesMock = new Mock<IRelationalDatabaseFacadeDependencies>();
Expand Down
29 changes: 29 additions & 0 deletions src/EntityFrameworkCore.Testing.NSubstitute.Tests/FeatureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Data.Common;
using EntityFrameworkCore.Testing.Common.Tests;
using EntityFrameworkCore.Testing.NSubstitute.Helpers;
using Microsoft.EntityFrameworkCore;
using NSubstitute;
using NUnit.Framework;

namespace EntityFrameworkCore.Testing.NSubstitute.Tests
{
public class FeatureTests : BaseForTests
{
[Test]
public void Set_DbConnection()
{
var mockConnection = Substitute.For<DbConnection>();
var mockedDbContext = new MockedDbContextBuilder<TestDbContext>().UseDbConnection(mockConnection).MockedDbContext;
var connection = mockedDbContext.Database.GetDbConnection();
Assert.That(connection, Is.EqualTo(mockConnection));
}

[Test]
public void Default_DbConnection()
{
var mockedDbContext = new MockedDbContextBuilder<TestDbContext>().MockedDbContext;
var connection = mockedDbContext.Database.GetDbConnection();
Assert.IsNull(connection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public override TDbContext Create()

var relationalConnection = Substitute.For<IRelationalConnection>();
relationalConnection.CommandTimeout.Returns(callInfo => 0);
relationalConnection.DbConnection.Returns(callInfo => DbConnection);

var dependencies = Substitute.For<IRelationalDatabaseFacadeDependencies>();
dependencies.ConcurrencyDetector.Returns(callInfo => concurrencyDetector);
Expand Down