Skip to content

Commit

Permalink
Merge pull request #39 from twsouthwick/builder
Browse files Browse the repository at this point in the history
Update to Autofac 5.2.0 and react to immutability changes
  • Loading branch information
mtrtm authored Aug 25, 2020
2 parents a00e862 + 49f0d2c commit 1e03574
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 219 deletions.
91 changes: 46 additions & 45 deletions AutofacContrib.NSubstitute.Tests/AutoSubstituteCollectionFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Autofac;
using NSubstitute;
using NUnit.Framework;

Expand Down Expand Up @@ -78,76 +79,76 @@ public TestIReadOnlyListComponent(IReadOnlyList<IServiceItem> serviceItems)
[Test]
public void TestIEnumerableCorrectlyResolves()
{
using(var autosub = new AutoSubstitute())
{
var mockA = autosub.Provide<IServiceItem, ServiceItemA>();
var mockB = autosub.Provide<IServiceItem, ServiceItemB>();
var component = autosub.Resolve<TestIEnumerableComponent>();
using var autosub = AutoSubstitute.Configure()
.Provide<IServiceItem, ServiceItemA>(out var mockA)
.Provide<IServiceItem, ServiceItemB>(out var mockB)
.Build();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
}
var component = autosub.Resolve<TestIEnumerableComponent>();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
}

[Test]
public void TestIListCorrectlyResolves()
{
using(var autosub = new AutoSubstitute())
{
var mockA = autosub.Provide<IServiceItem, ServiceItemA>();
var mockB = autosub.Provide<IServiceItem, ServiceItemB>();
var component = autosub.Resolve<TestIListComponent>();
using var autosub = AutoSubstitute.Configure()
.Provide<IServiceItem, ServiceItemA>(out var mockA)
.Provide<IServiceItem, ServiceItemB>(out var mockB)
.Build();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
}
var component = autosub.Resolve<TestIListComponent>();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
}

[Test]
public void TestIReadOnlyCollectionCorrectlyResolves()
{
using(var autosub = new AutoSubstitute())
{
var mockA = autosub.Provide<IServiceItem, ServiceItemA>();
var mockB = autosub.Provide<IServiceItem, ServiceItemB>();
var component = autosub.Resolve<TestIReadOnlyCollectionComponent>();
using var autosub = AutoSubstitute.Configure()
.Provide<IServiceItem, ServiceItemA>(out var mockA)
.Provide<IServiceItem, ServiceItemB>(out var mockB)
.Build();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
}
var component = autosub.Resolve<TestIReadOnlyCollectionComponent>();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
}

[Test]
public void TestICollectionCorrectlyResolves()
{
using(var autosub = new AutoSubstitute())
{
var mockA = autosub.Provide<IServiceItem, ServiceItemA>();
var mockB = autosub.Provide<IServiceItem, ServiceItemB>();
var component = autosub.Resolve<TestICollectionComponent>();
using var autosub = AutoSubstitute.Configure()
.Provide<IServiceItem, ServiceItemA>(out var mockA)
.Provide<IServiceItem, ServiceItemB>(out var mockB)
.Build();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
}
var component = autosub.Resolve<TestICollectionComponent>();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
}

[Test]
public void TestIReadOnlyListCorrectlyResolves()
{
using(var autosub = new AutoSubstitute())
{
var mockA = autosub.Provide<IServiceItem, ServiceItemA>();
var mockB = autosub.Provide<IServiceItem, ServiceItemB>();
var component = autosub.Resolve<TestIReadOnlyListComponent>();
using var autosub = AutoSubstitute.Configure()
.Provide<IServiceItem, ServiceItemA>(out var mockA)
.Provide<IServiceItem, ServiceItemB>(out var mockB)
.Build();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
}
var component = autosub.Resolve<TestIReadOnlyListComponent>();

Assert.That(component.ServiceItems, Is.Not.Empty);
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
}
}
}
58 changes: 27 additions & 31 deletions AutofacContrib.NSubstitute.Tests/AutoSubstituteFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,70 +49,66 @@ public void RunAll()
[Test]
public void DefaultConstructorIsLoose()
{
using (var mock = new AutoSubstitute())
{
RunWithSingleSetupationTest(mock);
}
using var mock = new AutoSubstitute();

RunWithSingleSetupationTest(mock);
}

[Test]
public void ProvideMock()
{
using (var autoSubstitute = new AutoSubstitute())
{
var mockA = Substitute.For<IServiceA>();
autoSubstitute.Provide(mockA);
var mockA = Substitute.For<IServiceA>();

var component = autoSubstitute.Resolve<TestComponent>();
component.RunAll();
using var autoSubstitute = AutoSubstitute.Configure()
.Provide(mockA)
.Build();

mockA.Received().RunA();
}
var component = autoSubstitute.Resolve<TestComponent>();
component.RunAll();

mockA.Received().RunA();
}

[Test]
public void ProvideImplementation()
{
using (var mock = new AutoSubstitute())
{
var serviceA = mock.Provide<IServiceA, ServiceA>();
using var mock = AutoSubstitute.Configure()
.Provide<IServiceA, ServiceA>(out var serviceA)
.Build();

Assert.IsNotNull(serviceA);
Assert.IsFalse(serviceA is ICallRouter);
}
Assert.IsNotNull(serviceA.Value);
Assert.IsFalse(serviceA.Value is ICallRouter);
}

[Test]
public void DefaultConstructorWorksWithAllTests()
{
using (var mock = new AutoSubstitute())
{
RunTest(mock);
}
using var mock = new AutoSubstitute();

RunTest(mock);
}

[Test]
public void WorksWithUnmetSetupations()
{
using (var loose = new AutoSubstitute())
{
RunWithSingleSetupationTest(loose);
}
using var loose = new AutoSubstitute();

RunWithSingleSetupationTest(loose);
}

[Test]
public void NormalSetupationsAreVerified()
{
using (var mock = new AutoSubstitute())
{
Assert.That(() => SetUpSetupations(mock), Throws.TypeOf<ReceivedCallsException>());
}
using var mock = new AutoSubstitute();

Assert.That(() => SetUpSetupations(mock), Throws.TypeOf<ReceivedCallsException>());
}

[Test]
public void ProperInitializationIsPerformed()
{
var autoSubstitute = new AutoSubstitute();
using var autoSubstitute = new AutoSubstitute();

Assert.IsNotNull(autoSubstitute.Container);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="Autofac" Version="5.2.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 32 additions & 22 deletions AutofacContrib.NSubstitute.Tests/ExampleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ConcreteClassWithDependency(IDependency1 dependency, int i)

public int Double()
{
return _dependency.SomeMethod(_i)*2;
return _dependency.SomeMethod(_i) * 2;
}
}

Expand Down Expand Up @@ -127,12 +127,15 @@ public void Example_test_with_standard_resolve()
public void Example_test_with_concrete_type_provided()
{
const int val = 3;
var AutoSubstitute = new AutoSubstitute();
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
AutoSubstitute.Provide<IDependency2, Dependency2>();
AutoSubstitute.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());

var result = AutoSubstitute.Resolve<MyClass>().AMethod();
using var mock = AutoSubstitute.Configure()
.Provide<IDependency2, Dependency2>(out _)
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
mock.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());

var result = mock.Resolve<MyClass>().AMethod();

Assert.That(result, Is.EqualTo(Dependency2.Value));
}
Expand All @@ -142,11 +145,14 @@ public void Example_test_with_concrete_object_provided()
{
const int val1 = 3;
const int val2 = 2;
var AutoSubstitute = new AutoSubstitute();
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
AutoSubstitute.Provide(new ConcreteClass(val2));

var result = AutoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
var mock = AutoSubstitute.Configure()
.Provide(new ConcreteClass(val2))
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);

var result = mock.Resolve<MyClassWithConcreteDependency>().AMethod();

Assert.That(result, Is.EqualTo(val1 + val2));
}
Expand All @@ -157,11 +163,14 @@ public void Example_test_with_substitute_for_concrete()
const int val1 = 3;
const int val2 = 2;
const int val3 = 10;
var AutoSubstitute = new AutoSubstitute();
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
AutoSubstitute.SubstituteFor<ConcreteClass>(val2).Add(Arg.Any<int>()).Returns(val3);

var result = AutoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
using var utoSubstitute = AutoSubstitute.Configure()
.SubstituteFor<ConcreteClass>(val2).Configure(c => c.Add(Arg.Any<int>()).Returns(val3))
.Build();

utoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);

var result = utoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();

Assert.That(result, Is.EqualTo(val3));
}
Expand All @@ -172,16 +181,17 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
const int val1 = 2;
const int val2 = 3;
const int val3 = 4;
var AutoSubstitute = new AutoSubstitute();
// Much better / more maintainable than:
//AutoSubstitute.SubstituteFor<ConcreteClassWithDependency>(AutoSubstitute.Resolve<IDependency1>(), val1);
AutoSubstitute.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1));
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
AutoSubstitute.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);

var result = AutoSubstitute.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
using var mock = AutoSubstitute.Configure()
.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
mock.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);

var result = mock.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();

Assert.That(result, Is.EqualTo(val2*val3*2));
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
}
}
}
9 changes: 6 additions & 3 deletions AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ public static void ShouldResolveASubstituteForIndexedDependency()
[Test]
public static void ShouldAcceptProvidedIndexedDependency()
{
var autoSubstitute = new AutoSubstitute();
var substitute = Substitute.For<IDependency2>();

using var autoSubstitute = AutoSubstitute.Configure()
.Provide(substitute, Switch.On)
.Build();

substitute.SomeOtherMethod().Returns(5);
autoSubstitute.Provide(substitute, Switch.On);


var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();

Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
Expand Down
3 changes: 2 additions & 1 deletion AutofacContrib.NSubstitute.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ VisualStudioVersion = 16.0.29409.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutofacContrib.NSubstitute", "AutofacContrib.NSubstitute\AutofacContrib.NSubstitute.csproj", "{7E69ECB4-84A5-41F0-8497-D94A3A45757F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutofacContrib.NSubstitute.Tests", "AutofacContrib.NSubstitute.Tests\AutofacContrib.NSubstitute.Tests.csproj", "{56FF83E0-BCB9-4391-9BCC-912347C84398}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutofacContrib.NSubstitute.Tests", "AutofacContrib.NSubstitute.Tests\AutofacContrib.NSubstitute.Tests.csproj", "{56FF83E0-BCB9-4391-9BCC-912347C84398}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A5ECD18F-1D42-444E-BB3A-6062D8B0E256}"
ProjectSection(SolutionItems) = preProject
BREAKING_CHANGES.md = BREAKING_CHANGES.md
global.json = global.json
LICENSE = LICENSE
logo.png = logo.png
README.md = README.md
Expand Down
Loading

0 comments on commit 1e03574

Please sign in to comment.