Skip to content

Commit f82ce4b

Browse files
committed
Fix test
1 parent 921d282 commit f82ce4b

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,31 @@ public async Task InvokeAsync_ScopePushed_BeforeConfiguringScope()
156156
[Fact]
157157
public async Task InvokeAsync_LocksScope_BeforeConfiguringScope()
158158
{
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));
159+
var verified = false;
160+
var scope = new Scope();
161+
_fixture.Hub
162+
.When(h => h.ConfigureScope(Arg.Any<Action<Scope>>()))
163+
.Do(
164+
Callback
165+
.First(c => c.ArgAt<Action<Scope>>(0).Invoke(scope))
166+
.Then(_ =>
167+
{
168+
Assert.True(scope.Locked);
169+
verified = true;
170+
}));
171+
172+
_fixture.Hub.When(h => h.ConfigureScope(Arg.Any<Action<Scope, Arg.AnyType>>(), Arg.Any<Arg.AnyType>()))
173+
.Do(Callback.First(c => c.InvokeGenericConfigureScopeMethod(scope)).Then(_ =>
174+
{
175+
Assert.True(scope.Locked);
176+
verified = true;
177+
}));
163178

164179
var sut = _fixture.GetSut();
165180

166181
await sut.InvokeAsync(_fixture.HttpContext, _fixture.RequestDelegate);
167182

168-
_fixture.Hub.Received().ConfigureScope(Arg.Any<Action<Scope>>());
183+
Assert.True(verified);
169184
}
170185

171186
[Fact]

test/Sentry.Testing/HubSubstituteExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ public static void SubstituteConfigureScope(
1010
.Do(c => c.Arg<Action<Scope>>().Invoke(scope));
1111

1212
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-
});
13+
.Do(c => c.InvokeGenericConfigureScopeMethod(scope));
14+
}
15+
16+
public static void InvokeGenericConfigureScopeMethod(
17+
this CallInfo c,
18+
Scope scope)
19+
{
20+
// If we use Arg.AnyType to look up the arguments, NSubstitute is unable to find
21+
// them. So as a workaround, we get the arguments directly and use reflection to
22+
// invoke the method.
23+
var action = c[0];
24+
var arg = c[1];
25+
action.GetType().GetMethod("Invoke")!.Invoke(action, [scope, arg]);
2226
}
2327
}

0 commit comments

Comments
 (0)