Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace MOQ with NSubstitute #1390

Merged
merged 4 commits into from Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/Autofac.Test/Autofac.Test.csproj
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<Using Include="Moq" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
</ItemGroup>

Expand Down Expand Up @@ -44,7 +44,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Expand Up @@ -11,12 +11,12 @@ public class CircularDependencyMiddlewareTests
[Fact]
public void NextCalled_OperationIsNotIDependencyTrackingResolveOperation_MiddlewareSkipped()
{
var resolveRequestContextMock = new Mock<ResolveRequestContext>();
var resolveRequestContextMock = Substitute.For<ResolveRequestContext>();
var middleware =
new CircularDependencyDetectorMiddleware(CircularDependencyDetectorMiddleware.DefaultMaxResolveDepth);

middleware.Execute(resolveRequestContextMock.Object, context => { });
middleware.Execute(resolveRequestContextMock, context => { });

resolveRequestContextMock.Verify(context => context.Operation.RequestDepth, Times.Never);
_ = resolveRequestContextMock.Received().Operation.RequestDepth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's another of those discard items here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected, as requested.

}
}
6 changes: 3 additions & 3 deletions test/Autofac.Test/Core/Resolving/ResolveOperationTests.cs
Expand Up @@ -19,14 +19,14 @@ public void NullLifetimeScope_ThrowsArgumentNullException()
[Fact]
public void NullDiagnosticSource_ThrowsArgumentNullException()
{
var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), null!));
var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(Substitute.For<ISharingLifetimeScope>(), null!));
Assert.Contains("(Parameter 'diagnosticSource')", ex.Message);
}

[Fact]
public void EmptyInProgessRequestWhenInitializing()
{
var resolveOperation = new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), new DiagnosticListener("SomeName"));
var resolveOperation = new ResolveOperation(Substitute.For<ISharingLifetimeScope>(), new DiagnosticListener("SomeName"));

var inProgressStack = resolveOperation.InProgressRequests;

Expand All @@ -36,7 +36,7 @@ public void EmptyInProgessRequestWhenInitializing()
[Fact]
public void GetOrCreateInstanceThrowsArgumentNullExceptionWhenResolveRequestIsNull()
{
var lifetimeScope = Mock.Of<ISharingLifetimeScope>();
var lifetimeScope = Substitute.For<ISharingLifetimeScope>();
var resolveOperation = new ResolveOperation(lifetimeScope, new DiagnosticListener("SomeName"));

Assert.Throws<ArgumentNullException>(() => resolveOperation.GetOrCreateInstance(lifetimeScope, null!));
Expand Down
Expand Up @@ -20,7 +20,7 @@ public void MiddlewareFailure_CorrectEventContent()
source.Subscribe(subscriber, subscriber.IsEnabled);

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

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

var context = MockResolveRequestContext();
source.MiddlewareSuccess(context, Mock.Of<IResolveMiddleware>());
source.MiddlewareSuccess(context, Substitute.For<IResolveMiddleware>());
var e = Assert.Single(subscriber.Events);
Assert.Equal(DiagnosticEventKeys.MiddlewareSuccess, e.Key);
Assert.IsType<MiddlewareDiagnosticData>(e.Value);
Expand Down Expand Up @@ -161,7 +161,7 @@ private static ResolveRequest MockResolveRequest()
{
return new ResolveRequest(
new TypedService(typeof(string)),
new ServiceRegistration(Mock.Of<IResolvePipeline>(), Mock.Of<IComponentRegistration>()),
new ServiceRegistration(Substitute.For<IResolvePipeline>(), Substitute.For<IComponentRegistration>()),
Enumerable.Empty<Parameter>());
}

Expand Down
20 changes: 10 additions & 10 deletions test/Autofac.Test/Features/Decorators/DecoratorMiddlewareTests.cs
Expand Up @@ -14,19 +14,19 @@ public class DecoratorMiddlewareTests
public void ResolveOperationDoesNotImplementIDependencyTrackingResolveOperation_DecoratorMiddlewareStoppedEarly()
{
var decoratorService = new DecoratorService(typeof(object));
var middleware = new DecoratorMiddleware(decoratorService, Mock.Of<IComponentRegistration>());
var contextMock = new Mock<ResolveRequestContext>();
var registrationMock = new Mock<IComponentRegistration>();
contextMock.Setup(context => context.Instance).Returns(new object());
contextMock.Setup(context => context.Registration).Returns(registrationMock.Object);
registrationMock.Setup(registration => registration.Options).Returns(RegistrationOptions.None);
var middleware = new DecoratorMiddleware(decoratorService, Substitute.For<IComponentRegistration>());
var contextMock = Substitute.For<ResolveRequestContext>();
var registrationMock = Substitute.For<IComponentRegistration>();
contextMock.Instance.Returns(new object());
contextMock.Registration.Returns(registrationMock);
registrationMock.Options.Returns(RegistrationOptions.None);

middleware.Execute(contextMock.Object, context => { });
middleware.Execute(contextMock, context => { });

contextMock.Verify(context => context.Instance, Times.Once);
contextMock.Verify(context => context.Registration.Options, Times.Once);
contextMock.Received(1);
registrationMock.Received(1);

// never got further because IResolveOperation is not of type IDependencyTrackingResolveOperation
contextMock.Verify(context => context.Service, Times.Never);
contextMock.DidNotReceive();
}
}
Expand Up @@ -17,7 +17,7 @@ public class TypeAssemblyReferenceProviderTests
[InlineData(typeof(IEnumerable<ContainerBuilder[]>), new[] { typeof(IEnumerable<>), typeof(ContainerBuilder) })]
[InlineData(typeof(IEnumerable<IIndex<int, Assert>>), new[] { typeof(IEnumerable<>), typeof(IIndex<,>), typeof(Assert) })]
[InlineData(typeof(DerivedClass), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert) })]
[InlineData(typeof(GenericDerivedClass<Assert>), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert), typeof(Mock) })]
[InlineData(typeof(GenericDerivedClass<Assert>), new[] { typeof(DerivedClass), typeof(RegistrationBuilder<,,>), typeof(Assert), typeof(object) })]
public void TypeReferencesCanBeDetermined(Type inputType, Type[] expandedTypeAssemblies)
{
var set = TypeAssemblyReferenceProvider.GetAllReferencedAssemblies(inputType);
Expand Down Expand Up @@ -59,7 +59,7 @@ public DerivedClass(Service defaultService, SimpleActivatorData activatorData, S
}

private class GenericDerivedClass<T>
: RegistrationBuilder<IIndex<T, Mock>, SimpleActivatorData, SingleRegistrationStyle>
: RegistrationBuilder<IIndex<T, object>, SimpleActivatorData, SingleRegistrationStyle>
{
public GenericDerivedClass(Service defaultService, SimpleActivatorData activatorData, SingleRegistrationStyle style)
: base(defaultService, activatorData, style)
Expand Down