Skip to content

Commit 5af8326

Browse files
authored
Merge pull request #1390 from aydjay/ajones-NSubstitute
Replace MOQ with NSubstitute
2 parents 1808508 + 8c32a76 commit 5af8326

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

test/Autofac.Test/Autofac.Test.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<Using Include="Moq" />
18+
<Using Include="NSubstitute" />
1919
<Using Include="Xunit" />
2020
</ItemGroup>
2121

@@ -44,7 +44,7 @@
4444
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4545
</PackageReference>
4646
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
47-
<PackageReference Include="Moq" Version="4.18.4" />
47+
<PackageReference Include="NSubstitute" Version="5.0.0" />
4848
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
4949
<PrivateAssets>all</PrivateAssets>
5050
</PackageReference>

test/Autofac.Test/Core/Resolving/Middleware/CircularDependencyMiddlewareTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class CircularDependencyMiddlewareTests
1111
[Fact]
1212
public void NextCalled_OperationIsNotIDependencyTrackingResolveOperation_MiddlewareSkipped()
1313
{
14-
var resolveRequestContextMock = new Mock<ResolveRequestContext>();
14+
var resolveRequestContextMock = Substitute.For<ResolveRequestContext>();
1515
var middleware =
1616
new CircularDependencyDetectorMiddleware(CircularDependencyDetectorMiddleware.DefaultMaxResolveDepth);
1717

18-
middleware.Execute(resolveRequestContextMock.Object, context => { });
18+
middleware.Execute(resolveRequestContextMock, context => { });
1919

20-
resolveRequestContextMock.Verify(context => context.Operation.RequestDepth, Times.Never);
20+
resolveRequestContextMock.Received();
2121
}
2222
}

test/Autofac.Test/Core/Resolving/ResolveOperationTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public void NullLifetimeScope_ThrowsArgumentNullException()
1919
[Fact]
2020
public void NullDiagnosticSource_ThrowsArgumentNullException()
2121
{
22-
var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), null!));
22+
var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(Substitute.For<ISharingLifetimeScope>(), null!));
2323
Assert.Contains("(Parameter 'diagnosticSource')", ex.Message);
2424
}
2525

2626
[Fact]
2727
public void EmptyInProgressRequestWhenInitializing()
2828
{
29-
var resolveOperation = new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), new DiagnosticListener("SomeName"));
29+
var resolveOperation = new ResolveOperation(Substitute.For<ISharingLifetimeScope>(), new DiagnosticListener("SomeName"));
3030

3131
var inProgressStack = resolveOperation.InProgressRequests;
3232

@@ -36,7 +36,7 @@ public void EmptyInProgressRequestWhenInitializing()
3636
[Fact]
3737
public void GetOrCreateInstanceThrowsArgumentNullExceptionWhenResolveRequestIsNull()
3838
{
39-
var lifetimeScope = Mock.Of<ISharingLifetimeScope>();
39+
var lifetimeScope = Substitute.For<ISharingLifetimeScope>();
4040
var resolveOperation = new ResolveOperation(lifetimeScope, new DiagnosticListener("SomeName"));
4141

4242
Assert.Throws<ArgumentNullException>(() => resolveOperation.GetOrCreateInstance(lifetimeScope, null!));

test/Autofac.Test/Diagnostics/DiagnosticSourceExtensionsTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void MiddlewareFailure_CorrectEventContent()
2020
source.Subscribe(subscriber, subscriber.IsEnabled);
2121

2222
var context = MockResolveRequestContext();
23-
source.MiddlewareFailure(context, Mock.Of<IResolveMiddleware>());
23+
source.MiddlewareFailure(context, Substitute.For<IResolveMiddleware>());
2424
var e = Assert.Single(subscriber.Events);
2525
Assert.Equal(DiagnosticEventKeys.MiddlewareFailure, e.Key);
2626
Assert.IsType<MiddlewareDiagnosticData>(e.Value);
@@ -35,7 +35,7 @@ public void MiddlewareStart_CorrectEventContent()
3535
source.Subscribe(subscriber, subscriber.IsEnabled);
3636

3737
var context = MockResolveRequestContext();
38-
source.MiddlewareStart(context, Mock.Of<IResolveMiddleware>());
38+
source.MiddlewareStart(context, Substitute.For<IResolveMiddleware>());
3939
var e = Assert.Single(subscriber.Events);
4040
Assert.Equal(DiagnosticEventKeys.MiddlewareStart, e.Key);
4141
Assert.IsType<MiddlewareDiagnosticData>(e.Value);
@@ -50,7 +50,7 @@ public void MiddlewareSuccess_CorrectEventContent()
5050
source.Subscribe(subscriber, subscriber.IsEnabled);
5151

5252
var context = MockResolveRequestContext();
53-
source.MiddlewareSuccess(context, Mock.Of<IResolveMiddleware>());
53+
source.MiddlewareSuccess(context, Substitute.For<IResolveMiddleware>());
5454
var e = Assert.Single(subscriber.Events);
5555
Assert.Equal(DiagnosticEventKeys.MiddlewareSuccess, e.Key);
5656
Assert.IsType<MiddlewareDiagnosticData>(e.Value);
@@ -161,7 +161,7 @@ private static ResolveRequest MockResolveRequest()
161161
{
162162
return new ResolveRequest(
163163
new TypedService(typeof(string)),
164-
new ServiceRegistration(Mock.Of<IResolvePipeline>(), Mock.Of<IComponentRegistration>()),
164+
new ServiceRegistration(Substitute.For<IResolvePipeline>(), Substitute.For<IComponentRegistration>()),
165165
Enumerable.Empty<Parameter>());
166166
}
167167

test/Autofac.Test/Features/Decorators/DecoratorMiddlewareTests.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public class DecoratorMiddlewareTests
1414
public void ResolveOperationDoesNotImplementIDependencyTrackingResolveOperation_DecoratorMiddlewareStoppedEarly()
1515
{
1616
var decoratorService = new DecoratorService(typeof(object));
17-
var middleware = new DecoratorMiddleware(decoratorService, Mock.Of<IComponentRegistration>());
18-
var contextMock = new Mock<ResolveRequestContext>();
19-
var registrationMock = new Mock<IComponentRegistration>();
20-
contextMock.Setup(context => context.Instance).Returns(new object());
21-
contextMock.Setup(context => context.Registration).Returns(registrationMock.Object);
22-
registrationMock.Setup(registration => registration.Options).Returns(RegistrationOptions.None);
17+
var middleware = new DecoratorMiddleware(decoratorService, Substitute.For<IComponentRegistration>());
18+
var contextMock = Substitute.For<ResolveRequestContext>();
19+
var registrationMock = Substitute.For<IComponentRegistration>();
20+
contextMock.Instance.Returns(new object());
21+
contextMock.Registration.Returns(registrationMock);
22+
registrationMock.Options.Returns(RegistrationOptions.None);
2323

24-
middleware.Execute(contextMock.Object, context => { });
24+
middleware.Execute(contextMock, context => { });
2525

26-
contextMock.Verify(context => context.Instance, Times.Once);
27-
contextMock.Verify(context => context.Registration.Options, Times.Once);
26+
contextMock.Received(1);
27+
registrationMock.Received(1);
2828

2929
// never got further because IResolveOperation is not of type IDependencyTrackingResolveOperation
30-
contextMock.Verify(context => context.Service, Times.Never);
30+
contextMock.DidNotReceive();
3131
}
3232
}

test/Autofac.Test/Util/Cache/TypeAssemblyReferenceProviderTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TypeAssemblyReferenceProviderTests
1717
[InlineData(typeof(IEnumerable<ContainerBuilder[]>), new[] { typeof(IEnumerable<>), typeof(ContainerBuilder) })]
1818
[InlineData(typeof(IEnumerable<IIndex<int, Assert>>), new[] { typeof(IEnumerable<>), typeof(IIndex<,>), typeof(Assert) })]
1919
[InlineData(typeof(DerivedClass), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert) })]
20-
[InlineData(typeof(GenericDerivedClass<Assert>), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert), typeof(Mock) })]
20+
[InlineData(typeof(GenericDerivedClass<Assert>), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert), typeof(object) })]
2121
public void TypeReferencesCanBeDetermined(Type inputType, Type[] expandedTypeAssemblies)
2222
{
2323
var set = TypeAssemblyReferenceProvider.GetAllReferencedAssemblies(inputType);
@@ -59,7 +59,7 @@ public DerivedClass(Service defaultService, SimpleActivatorData activatorData, S
5959
}
6060

6161
private class GenericDerivedClass<T>
62-
: RegistrationBuilder<IIndex<T, Mock>, SimpleActivatorData, SingleRegistrationStyle>
62+
: RegistrationBuilder<IIndex<T, object>, SimpleActivatorData, SingleRegistrationStyle>
6363
{
6464
public GenericDerivedClass(Service defaultService, SimpleActivatorData activatorData, SingleRegistrationStyle style)
6565
: base(defaultService, activatorData, style)

0 commit comments

Comments
 (0)