Skip to content

Commit d117255

Browse files
Fix test dependency registrations (#933)
Co-authored-by: David Boike <david.boike@gmail.com>
1 parent 6165a2a commit d117255

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

src/NServiceBus.Storage.MongoDB.AcceptanceTests/When_custom_provider_registered.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ public class When_custom_provider_registered : NServiceBusAcceptanceTest
1818
public async Task Should_be_used()
1919
{
2020
Context context = await Scenario.Define<Context>()
21-
.WithEndpoint<EndpointWithCustomProvider>(b => b.When(session => session.SendLocal(new StartSaga1 { DataId = Guid.NewGuid() })))
21+
.WithEndpoint<EndpointWithCustomProvider>(b =>
22+
{
23+
b.Services(c =>
24+
c.AddSingleton<IMongoClientProvider>(b => new CustomProvider(b.GetRequiredService<Context>())), afterStart: true);
25+
b.When(session => session.SendLocal(new StartSaga1
26+
{
27+
DataId = Guid.NewGuid()
28+
}));
29+
})
2230
.Done(c => c.SagaReceivedMessage)
2331
.Run();
2432

@@ -34,11 +42,7 @@ public class Context : ScenarioContext
3442
public class EndpointWithCustomProvider : EndpointConfigurationBuilder
3543
{
3644
public EndpointWithCustomProvider() =>
37-
EndpointSetup<DefaultServer>(config =>
38-
{
39-
config.RegisterComponents(c =>
40-
c.AddSingleton<IMongoClientProvider>(b => new CustomProvider(b.GetRequiredService<Context>())));
41-
});
45+
EndpointSetup<DefaultServer>();
4246

4347
public class JustASaga(Context testContext) : Saga<JustASagaData>, IAmStartedByMessages<StartSaga1>
4448
{
@@ -53,30 +57,30 @@ public Task Handle(StartSaga1 message, IMessageHandlerContext context)
5357
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<JustASagaData> mapper) => mapper.MapSaga(s => s.DataId).ToMessage<StartSaga1>(m => m.DataId);
5458
}
5559

56-
public class CustomProvider(Context testContext) : IMongoClientProvider
60+
public class JustASagaData : ContainSagaData
61+
{
62+
public virtual Guid DataId { get; set; }
63+
}
64+
}
65+
66+
public class CustomProvider(Context testContext) : IMongoClientProvider
67+
{
68+
[field: AllowNull, MaybeNull]
69+
public IMongoClient Client
5770
{
58-
[field: AllowNull, MaybeNull]
59-
public IMongoClient Client
71+
get
6072
{
61-
get
73+
if (field is not null)
6274
{
63-
if (field is not null)
64-
{
65-
return field;
66-
}
67-
68-
var containerConnectionString = Environment.GetEnvironmentVariable("NServiceBusStorageMongoDB_ConnectionString");
69-
70-
field = string.IsNullOrWhiteSpace(containerConnectionString) ? new MongoClient() : new MongoClient(containerConnectionString);
71-
testContext.ProviderWasCalled = true;
7275
return field;
7376
}
74-
}
75-
}
7677

77-
public class JustASagaData : ContainSagaData
78-
{
79-
public virtual Guid DataId { get; set; }
78+
var containerConnectionString = Environment.GetEnvironmentVariable("NServiceBusStorageMongoDB_ConnectionString");
79+
80+
field = string.IsNullOrWhiteSpace(containerConnectionString) ? new MongoClient() : new MongoClient(containerConnectionString);
81+
testContext.ProviderWasCalled = true;
82+
return field;
83+
}
8084
}
8185
}
8286

src/NServiceBus.Storage.MongoDB.AcceptanceTests/When_using_outbox_synchronized_session_via_container.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ public partial class When_using_outbox_synchronized_session_via_container : NSer
1414
public async Task Should_inject_synchronized_session_into_handler()
1515
{
1616
var context = await Scenario.Define<Context>()
17-
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage())))
17+
.WithEndpoint<Endpoint>(b =>
18+
{
19+
b.Services(c =>
20+
{
21+
c.AddScoped<MyRepository>();
22+
});
23+
b.When(s => s.SendLocal(new MyMessage()));
24+
})
1825
.Done(c => c.Done)
1926
.Run()
2027
.ConfigureAwait(false);
@@ -40,10 +47,6 @@ public Endpoint()
4047
config.ConfigureTransport().TransportTransactionMode = TransportTransactionMode.ReceiveOnly;
4148

4249
config.EnableOutbox();
43-
config.RegisterComponents(c =>
44-
{
45-
c.AddScoped<MyRepository>();
46-
});
4750
});
4851
}
4952

0 commit comments

Comments
 (0)