Skip to content

Commit 364d8a3

Browse files
committed
template: update mocker setup
1 parent 3971098 commit 364d8a3

File tree

1 file changed

+14
-5
lines changed
  • templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data.Test/Utilities

1 file changed

+14
-5
lines changed

templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Data.Test/Utilities/TestBase.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,21 @@ protected void PersistentSetup(Action<AutoMocker> setup)
7979
private MockerScope BeginMockScope(bool standalone = false)
8080
{
8181
var mocker = new MockerScope(standalone ? null : this);
82-
var db = new AppDbContextForSqlite(DbFixture.Options);
82+
83+
var dbOptions = new DbContextOptionsBuilder<AppDbContext>(DbFixture.Options)
84+
.UseApplicationServiceProvider(mocker).Options;
85+
86+
var db = new AppDbContextForSqlite(dbOptions);
8387
#if Tenancy
8488
db.TenantId = db.Tenants.OrderBy(t => t.TenantId).First().TenantId;
8589
#endif
8690

87-
mocker.Use(DbFixture.Options);
91+
// Expose the current user to EF extensions (if any).
92+
mocker.GetMock<IHttpContextAccessor>()
93+
.SetupGet(a => a.HttpContext)
94+
.Returns(() => new DefaultHttpContext { User = CurrentUser });
95+
96+
mocker.Use(dbOptions);
8897
mocker.Use<AppDbContext>(db);
8998

9099
mocker.Use<CrudContext<AppDbContext>>(new CrudContext<AppDbContext>(
@@ -94,11 +103,11 @@ private MockerScope BeginMockScope(bool standalone = false)
94103

95104
mocker.GetMock<IDbContextFactory<AppDbContext>>()
96105
.Setup(x => x.CreateDbContextAsync(It.IsAny<CancellationToken>()))
97-
.ReturnsAsync(() => new AppDbContextForSqlite(DbFixture.Options));
106+
.ReturnsAsync(() => new AppDbContextForSqlite(dbOptions));
98107

99108
mocker.GetMock<IDbContextFactory<AppDbContext>>()
100109
.Setup(x => x.CreateDbContext())
101-
.Returns(() => new AppDbContextForSqlite(DbFixture.Options));
110+
.Returns(() => new AppDbContextForSqlite(dbOptions));
102111

103112
mocker.Use<IMemoryCache>(new MemoryCache(new MemoryCacheOptions()));
104113

@@ -121,7 +130,7 @@ public class MockerScope : AutoMocker, IDisposable
121130
{
122131
private readonly TestBase? _Parent;
123132

124-
public MockerScope(TestBase? parent) : base(MockBehavior.Loose)
133+
public MockerScope(TestBase? parent) : base(MockBehavior.Loose, DefaultValue.Empty, callBase: true)
125134
{
126135
_Parent = parent;
127136
if (parent != null) parent._CurrentMocker = this;

0 commit comments

Comments
 (0)