Skip to content

Commit 2456363

Browse files
committed
Substitute both ConfigureScope overloads in tests
1 parent 96c41ee commit 2456363

File tree

13 files changed

+47
-39
lines changed

13 files changed

+47
-39
lines changed

test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ private class Fixture
3131
public Fixture()
3232
{
3333
Scope = new();
34-
Hub.When(hub => hub.ConfigureScope(Arg.Any<Action<Scope>>()))
35-
.Do(callback => callback.Arg<Action<Scope>>().Invoke(Scope));
34+
Hub.SubstituteConfigureScope(Scope);
3635

3736
Hub.When(hub => hub.CaptureEvent(Arg.Any<SentryEvent>(), Arg.Any<Scope>()))
3837
.Do(_ => Scope.Evaluate());
@@ -157,23 +156,16 @@ public async Task InvokeAsync_ScopePushed_BeforeConfiguringScope()
157156
[Fact]
158157
public async Task InvokeAsync_LocksScope_BeforeConfiguringScope()
159158
{
160-
var verified = false;
161-
var scope = new Scope();
162-
_fixture.Hub
163-
.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
164-
.Do(Callback
165-
.First(c => c.ArgAt<Action<Scope>>(0)(scope))
166-
.Then(_ =>
167-
{
168-
Assert.True(scope.Locked);
169-
verified = true;
170-
}));
159+
var scopeLocked = false;
160+
_fixture.Hub.When(h => h.PushAndLockScope()).Do(_ => scopeLocked = true);
161+
_fixture.Hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
162+
.Do(_ => Assert.True(scopeLocked));
171163

172164
var sut = _fixture.GetSut();
173165

174166
await sut.InvokeAsync(_fixture.HttpContext, _fixture.RequestDelegate);
175167

176-
Assert.True(verified);
168+
_fixture.Hub.Received().ConfigureScope(Arg.Any<Action<Scope>>());
177169
}
178170

179171
[Fact]
@@ -182,8 +174,7 @@ public async Task InvokeAsync_OnEvaluating_HttpContextDataSet()
182174
const string expectedTraceIdentifier = "trace id";
183175
_ = _fixture.HttpContext.TraceIdentifier.Returns(expectedTraceIdentifier);
184176
var scope = new Scope();
185-
_fixture.Hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
186-
.Do(c => c.Arg<Action<Scope>>()(scope));
177+
_fixture.Hub.SubstituteConfigureScope(scope);
187178

188179
var sut = _fixture.GetSut();
189180

@@ -200,8 +191,7 @@ public async Task InvokeAsync_OnEvaluatingClonedScope_HttpContextDataSet()
200191
const string expectedTraceIdentifier = "trace id";
201192
_ = _fixture.HttpContext.TraceIdentifier.Returns(expectedTraceIdentifier);
202193
var scope = new Scope();
203-
_fixture.Hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
204-
.Do(c => c.Arg<Action<Scope>>()(scope));
194+
_fixture.Hub.SubstituteConfigureScope(scope);
205195

206196
var sut = _fixture.GetSut();
207197

@@ -664,7 +654,7 @@ public async Task InvokeAsync_InstrumenterOpenTelemetry_SavesScope()
664654
// Arrange
665655
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
666656
var scope = new Scope();
667-
_fixture.Hub.ConfigureScope(Arg.Do<Action<Scope>>(action => action.Invoke(scope)));
657+
_fixture.Hub.SubstituteConfigureScope(scope);
668658
var sut = _fixture.GetSut();
669659
var activity = new Activity("test").Start();
670660

test/Sentry.EntityFramework.Tests/SentryQueryLoggerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public void Log_QueryLogger_CaptureEvent()
88
var scope = new Scope(new SentryOptions());
99
var hub = Substitute.For<IHub>();
1010
hub.IsEnabled.Returns(true);
11-
hub.ConfigureScope(Arg.Invoke(scope));
11+
hub.SubstituteConfigureScope(scope);
1212

1313
var expected = new
1414
{

test/Sentry.Extensions.Logging.Tests/SentryLoggerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private class Fixture
1414
public Fixture()
1515
{
1616
_ = Hub.IsEnabled.Returns(true);
17-
Hub.ConfigureScope(Arg.Invoke(Scope));
17+
Hub.SubstituteConfigureScope(Scope);
1818
}
1919

2020
public SentryLogger GetSut() => new(CategoryName, Options, new MockClock(), Hub);

test/Sentry.Log4Net.Tests/SentryAppenderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private class Fixture
1616
public Fixture()
1717
{
1818
HubAccessor = () => Hub;
19-
Hub.ConfigureScope(Arg.Invoke(Scope));
19+
Hub.SubstituteConfigureScope(Scope);
2020
InitAction = s =>
2121
{
2222
DsnReceivedOnInit = s;

test/Sentry.NLog.Tests/SentryTargetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Fixture()
2323
Hub.IsEnabled.Returns(true);
2424
HubAccessor = () => Hub;
2525
Scope = new Scope(new SentryOptions());
26-
Hub.ConfigureScope(Arg.Invoke(Scope));
26+
Hub.SubstituteConfigureScope(Scope);
2727
}
2828

2929
public Target GetTarget(bool asyncTarget = false)

test/Sentry.OpenTelemetry.Tests/AspNetCoreEnricherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public void Enrich_SendDefaultPii_UserOnScope()
99
var scope = new Scope();
1010
var options = new SentryOptions { SendDefaultPii = true };
1111
var hub = Substitute.For<IHub>();
12-
hub.ConfigureScope(Arg.Do<Action<Scope>>(action => action(scope)));
12+
hub.SubstituteConfigureScope(scope);
1313

1414
var user = new SentryUser { Id = "foo" };
1515
var userFactory = Substitute.For<ISentryUserFactory>();

test/Sentry.Serilog.Tests/SentrySinkTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Fixture()
1414
{
1515
Hub.IsEnabled.Returns(true);
1616
HubAccessor = () => Hub;
17-
Hub.ConfigureScope(Arg.Invoke(Scope));
17+
Hub.SubstituteConfigureScope(Scope);
1818
}
1919

2020
public SentrySink GetSut()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Sentry.Testing;
2+
3+
public static class HubSubstituteExtensions
4+
{
5+
public static void SubstituteConfigureScope(
6+
this IHub hub,
7+
Scope scope)
8+
{
9+
hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
10+
.Do(c => c.Arg<Action<Scope>>().Invoke(scope));
11+
12+
hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope, Arg.AnyType>>(), Arg.Any<Arg.AnyType>()))
13+
.Do(c =>
14+
{
15+
// If we use Arg.AnyType to look up the arguments, NSubstitute is unable to find
16+
// them. So as a workaround, we get the arguments directly and use reflection to
17+
// invoke the method.
18+
var action = c[0];
19+
var arg = c[1];
20+
action.GetType().GetMethod("Invoke")!.Invoke(action, [scope, arg]);
21+
});
22+
}
23+
}

test/Sentry.Tests/Extensibility/HubAdapterTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ private void TestAddBreadcrumbExtension(
162162
const BreadcrumbLevel level = BreadcrumbLevel.Critical;
163163

164164
var scope = new Scope();
165-
Hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
166-
.Do(c => c.Arg<Action<Scope>>()(scope));
165+
Hub.SubstituteConfigureScope(scope);
167166

168167
action(message, category, type, data, level);
169168

test/Sentry.Tests/HubExtensionsTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ public class HubExtensionsTests
77

88
public HubExtensionsTests()
99
{
10-
Sut.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
11-
.Do(c => c.Arg<Action<Scope>>()(Scope));
10+
Sut.SubstituteConfigureScope(Scope);
1211
}
1312

1413
[Fact]

0 commit comments

Comments
 (0)